# Why an Overflow Message ?

Can anyone help with the below. I've never sees this before !

Add the following lines of code to a new project and run :-

Dim lngTest As Long
lngTest = 12 * 3600

This produces as "Overflow" which I can only get around with :-

Dim lngTest As Long
lngTest = 3600
lngTest = 12 * lngTest

Wierd or am I being Thick ?

Many Thanks

Dave
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
That is wierd.
Interested to know the answer also.
0
Commented:
Hi Dave
Strange behaviour. It seems that it should work.
It doesn't seem that the variable lngTest is the problem since I get the same overflow error in the Debug window by typing "? 12 * 3600" so I went to msdn help and they say to write the code in the manner below. Lo and behold it works!!!

Dim lngTest As Long
lngTest = 12 * CLng(3600)

Happy New Year

Sicilian

0
Commented:
I have tested this thoroughly and have deermined that VB is interpreting the literal 36000 as an INTEGER rather than long. It seems that VB sees 12 (well with the range of integer) and then 36000 (Long integer) and can't decide which way to go with it. Weird.

Good luck!!
0
Commented:
Yep. You have to tell VB that 12 is not an integer but a long. Enter it as:

LngTest = 12# * 3600

Since the first arguement in the equation is a long, the entire arguement will be processed as a long. Even tho you explicitly declare LngTest to be long, the equation coerces it into integer as that is all that is on the right side of the assignment.

This is not a "bug", but a "side effect" of how the compiler optimizes and "helps" with implicit typing.

M
0

Experts Exchange Solution brought to you by