Search code examples
vb.netoverflowulong

Numeric value of ULong.MaxValue is considered an overflow in Visual Basic.NET


In Visual Basic.NET ULong.MaxValue=18,446,744,073,709,551,615.

The following code runs fine:

Dim a As ULong = ULong.MaxValue

The following code returns an overflow error on the number

Dim b As ULong = 18446744073709551615

What is causing this error?


Solution

  • The compiler assumes that any whole number too large to be an Integer is a Long and of course your number won't fit in a Long. You need to add the UL suffix to the literal number to indicate that it is ULong and not just Long.

    Dim b As ULong = 18446744073709551615UL