tags:

views:

97

answers:

2

This work nicely

Public Const test As ULong = 1 << 30

This doesn't work nicely

Public Const test As ULong = 1 << 31

It create this error:

Constant expression not representable in type 'ULong'

Anyone know how to make it work?

This does work:

Public Const test As Long = 1 << 31

But I have to use ULong

+2  A: 

Try the following:

Public Const test As ULong = 1UL << 31 

You need to explicitly tell the compiler you are doing the operation on a ULong.

The C# equivalent works:

public const ulong test  = 1UL << 31;
Kelsey
+1 and thanks, good to know!
Fredou
+4  A: 

You cannot shift 1 << 31 with a Long datatype, so you get this error.

However, this is because 1, as an integer literal, is treated as an Int32, which is the default integer literal.

You should work around this by defining this as:

Public Const test As ULong = 1UL << 30
Public Const test2 As ULong = 1UL << 31

The UL flag says to make the 1 an unsigned long. See Type characters for details.

Reed Copsey
Your link is broke ;)
Aequitarum Custos
+1, You would have got the green check but Kelsey was faster
Fredou
@Aequitarum: Thanks - fixed.
Reed Copsey
@Fredou: I go for quality, not speed. Being first shouldn't make something the answer - it should be about being correct, and explaining the issue well... Read the FAQ - it suggests waiting a few hours before marking an answer, since you should pick the "best" answer, not the fastest answer.
Reed Copsey
+1 for being didatic.
Paulo Santos
@Reed, for me, in this case, both answer are equal since both give the **UL** keyword and the same explanation which I had to explicitly say: this number is a ULong, not something else.
Fredou
@Fredou: Oh, that's great. I'm not saying to switch - just saying "faster isn't better"...
Reed Copsey