|
-
Jul 9th, 2004, 03:42 AM
#1
Thread Starter
Fanatic Member
weird overflow error...
hi all, could someone tell me why:
VB Code:
Dim x As Integer, a As Byte, b As Byte
a = 0
b = 1
x = a - b
generates an overflow error on last line, while:
VB Code:
Dim x As Integer, a As Byte, b As Byte
a = 0
b = 1
x = a + b
does not. i have to modify first operation into:
VB Code:
Dim x As Integer, a As Byte, b As Byte
a = 0
b = 1
x = a * 1 - b * 1
to make it work. this is really weird.
thanks,
wc.
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Jul 9th, 2004, 03:55 AM
#2
Hyperactive Member
change data type... maybe
Born to help others
(If I've been helpful then please rate my post. Thanks)
call me EJ or be slapped! 
-
Jul 9th, 2004, 03:56 AM
#3
Thread Starter
Fanatic Member
that's the whole point... x IS an integer value.
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Jul 9th, 2004, 03:56 AM
#4
The problem is that by default all calculatations are performed in the smallest data type that is appropriate, and as both variables are bytes then the calculation is done in bytes.
However, if you look in MSDN for the limits of the byte data type:
Data type Storage size Range
Byte 1 byte 0 to 255
and -1 is outside of the valid range for the data type.
The "* 1" you added to make it work fixes this by doing an Implicit data type conversion, an explicit one would be better (as the compiler can change what method it uses rather than doing extra work), eg:
x = CInt(a) - CInt(b)
-
Jul 9th, 2004, 04:02 AM
#5
Thread Starter
Fanatic Member
agreed on the fact that the overflow is generated by the fact that 0 - 1 = -1 is outside the range of bytes. agreed also on the implicit conversion:
-> that's why i've added the *1 multiplication in the first place (and you are right, better do an explicit conversion).
however, the fact is that x IS AN INTEGER!
i've declared it as such, therefore i still can scarcely understand why this is generating an error. i believe this is a small memory management leak in vb6, assuming to compute on the smallest type even if the user (me) has correctly allowed memory space according on presumed results.
When your car breaks down,
close all windows and retry 
=> please rate all users posts! <=
-
Jul 9th, 2004, 04:07 AM
#6
Where the results are going has no effect whatsoever on how the calculation is performed 
Even though it is only one line of code, there are several steps that take place "behind the scenes". Essentially the calculation is done first (which is where the error occurs), and then the result is returned to the required location (eg: your x variable).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|