views:

121

answers:

4

Pretty simple.

UInteger data type hold any value between 0 and 4,294,967,295. MSDN.

If I try this code in VB.NET I get a compiler error:

Dim Test As UInteger = &HFFFFFFFF

Error: "Constant expression not representable in type 'UInteger'.

Why I can't set 0xFFFFFFFF (4,294,967,295) to a UInteger if this type can hold this value?

+11  A: 

I believe it's because the literal &HFFFFFFFF is interpreted by the VB.NET compiler as an Integer, and that value for an Integer is a negative number (-1), which obviously can't be cast to a UInteger.

This issue is easily fixed by writing &HFFFFFFFFUI, appending the UI suffix to treat the literal as a UInteger.

Dan Tao
Wow. Just... wow.
Thanatos
+4  A: 

You could use the MaxValue constant:

Dim Test As UInteger = UInteger.MaxValue
Darin Dimitrov
+2  A: 

Looking at this article, it appears the solution is to set the value as &HFFFFFFFFui, since according the article:

If you just write &HFFFFFFFF then it is treated as a signed 32 bit integer, value is -1, and you can't assign that to a UInteger.

If you write &HFFFFFFFFL then it is treated as a signed 64 bit integer, now the binary is: 0000000000000000000000000000000011111111111111111111111111111111

EDIT: Updated answer to match 0xA3's recommendation.

Dillie-O
0xA3
Great insight. I updated the bold part accordingly. The article (forum) was a few years old.
Dillie-O
A: 

Incidentally, it's worth nothing that "LongValue = LongValue And Not &h8000" just clears one bit in 'LongValue', as does "LongValue = LongValue And Not &h800000000000". On the other hand, "LongValue = LongValue And Not &h80000000" will clear out the top 33 bits of LongValue.

supercat