• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

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
0
crowleyd
Asked:
crowleyd
1 Solution
 
crazymanCommented:
That is wierd.
Interested to know the answer also.
0
 
SicilianCommented:
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
 
DrDelphiCommented:
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
 
mark2150Commented:
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
 
amebaCommented:
>12# * 3600
or
12& * 3600&
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now