NormD
NormD

Reputation: 549

How to define 64 bit constants in VB?

In Visual Basic

Friend Const xxx As UInt64 = 400 * 365 * 24 * 60 * 60 ''// Number of secs in 400 years

This fails with the error

constant expression not representable in type integer

The problem is 400 * 365 * 24 * 60 * 60 is larger than 2^32

I would have thought that by declaring the constant to be UInt64 that it would be OK to assign a 64 bit value to it

Upvotes: 2

Views: 2736

Answers (2)

LarryF
LarryF

Reputation: 5083

Put a hash at the end of the constant, and declare it as a 'double'...

I did this with my bitmask fields:

Public Const EDIT_TRANSACTION              As Double = 1073741824
Public Const EDIT_DWRDELIVERY              As Double = 2147483648#
Public Const ENTER_DWRORDER                As Double = 4294967296#
Public Const DELETE_DWRORDER               As Double = 8589934592#
Public Const DELETE_TRANSACTION            As Double = 17179869184#
Public Const DELETE_WATERORDER             As Double = 34359738368#
Public Const ENTER_METERREADING            As Double = 68719476736#

** EDIT **

I guess I got marked down on this because this was old code I wrote for VB6, and not exactly what you were asking for. So, if anyone reading this is using VB6, and have to pass Bitmask fields to something like SQL, this is what worked perfectly for me.

Otherwise, just keep voting my answer down. :)

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415630

Aside from the fact that there are slightly more than 365 days each year (you need to add 97 leap days), each of the values that are multiplied to make up your constant are integer literals, and therefore until you assign them to the UInt64 it's all done in integer space. Try this:

Friend Const xxx As UInt64 = 400UL * 365UL * 24UL * 60UL * 60UL

Upvotes: 7

Related Questions