Link to home
Start Free TrialLog in
Avatar of pcdev
pcdev

asked on

simple calculation wont work help please

This just will not work in visual basic 5 on a windows
2000 pc with sp2 on it

Gtop=7680
Gleft=19200
Gwidth=0
Gheight=7680

If (Abs(Gtop) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then msgbox "OK"



I get a runtime error 6 - overflow
any thoughts???
many thanks
paul@pcdevelopments.co.uk
   
Avatar of rspahitz
rspahitz
Flag of United States of America image

Are your variables defined as long, or integer?  If integer, then you're overflowing past the 32K limit.
you must define the variables of type long as rspahitz said
ASKER CERTIFIED SOLUTION
Avatar of jklmn
jklmn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dim Gtop As Long
Dim Gleft As Long
Dim Gwidth As Long
Dim Gheight As Long

Gtop = 7680
Gleft = 19200
Gwidth = 0
Gheight = 7680

If (Abs(Gtop) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then MsgBox "OK"

oops, didn't mean to repeat an answer.
Private Sub Command1_Click()
Gtop = 7680
Gleft = 19200
Gwidth = 0
Gheight = 7680

If (Abs(CLng(Gtop)) + Abs(Gleft) + Abs(Gwidth) + Abs(Gheight)) > 0 Then MsgBox "OK"




End Sub
Avatar of pcdev
pcdev

ASKER

They were all dim as integer
changed them all to long same thing
also if you do this in the debug window
?7680+19200+7680
I get the same message, no dims, no variable etc??

So i have accepted the answer from gbaren

thanks very much
Avatar of pcdev

ASKER

They were all dim as integer
changed them all to long same thing
also if you do this in the debug window
?7680+19200+7680
I get the same message, no dims, no variable etc??

So i have accepted the answer from gbaren

thanks very much
deighton's comment is interesting because it applies the conversion to the 'if', thereby making the addition become long.

In some cases this would be appropriate, but in your case, pcdev, assuming that these variables represent screen dimensions, they should be defined as long.

Also, as jklmn commented, your 'if' would probably be better served as 4 separate comparisons rather than a comparison of the sums.  Besides, what if the gLeft is negative, which is possible for screen dimension.


>represent screen dimensions, they should be defined as long.
Although I agree with this, VB uses singles most of the time.

Interestingly enough doing it in the debug will give you an error becuase a 'nude' number is implicitly typed as the smallest type which can hold it (but not byte). If you want to force an long addition, just use

?7680&+19200+7680
Note you only need the & on the first term (as deighton pointed out), this is enough to switch the addition to longs.

If I remember correctly, & forces long, # double, % integer. Can't remember if there are any others offhand.

Just an aside really.
"VB uses singles most of the time"
Actually, you're right.  They should be defined as single!!!

"& forces long, # double, % integer"

Yes, and ! is single, and $ is string.

I use long a lot for internal calculations, because it's the quickest type. Forgot about single !s, but surely quotes are used for string constants ;)