Justin808
Justin808

Reputation: 21512

C#, Cannot implicitly convert type `long' to `uint'

Whats wrong with this? I get the error on the top two lines.

bounds.xMin = (Mathf.FloorToInt (x / GridSize) * GridSize);
bounds.yMin = (Mathf.FloorToInt (y / GridSize) * GridSize);
bounds.xMax = bounds.xMin + GridSize;
bounds.yMax = bounds.yMin + GridSize;

bounds.xMin, bounds.yMin, x, y and GridSize are all type uint.

Upvotes: 1

Views: 3824

Answers (1)

Jakub Lortz
Jakub Lortz

Reputation: 14896

All your variables are uint, but the return type of Mathf.FloorToInt is int.

To multiply an uint by an int, C# will convert both operands to long, as it's the only integer type that can represent all their possible values. The result of this operation will also be long.

You will need to cast the whole result or at least the result of Mathf.FloorToInt to uint.

From C# language spec:

For the binary +, –, *, /, %, &, ^, |, ==, !=, >, <, >=, and <= operators, the operands are converted to type T, where T is the first of int, uint, long, and ulong that can fully represent all possible values of both operands. The operation is then performed using the precision of type T, and the type of the result is T

Upvotes: 4

Related Questions