Long = Integer * Integer Overflow error 
Author Message
 Long = Integer * Integer Overflow error

The following produces an overflow error if c is greater
than 32,767.  It appears as if the long data type is being
changed to an Integer.  Why?
Dim a As Integer
Dim b As Integer
Dim c As Long
a = 365
b = 90
c = a * b


Sat, 25 Sep 2004 05:16:48 GMT  
 Long = Integer * Integer Overflow error

Quote:
> The following produces an overflow error if c is greater
> than 32,767.  It appears as if the long data type is being
> changed to an Integer.  Why?
> Dim a As Integer
> Dim b As Integer
> Dim c As Long
> a = 365
> b = 90
> c = a * b

VB evaluates a*b before it looks to see where you are storing the result.
When it does that it uses a temporary data type that matches the largest
type used in the expression so the action is really more like:
   <tempvar>=a*b
   c=<tempvar>
and in this case <tempvar> is defined as Integer so you get an overflow.
The workaround is to "upgrade" at least one operand in the expression:
   c=a*clng(b)


Sat, 25 Sep 2004 05:58:05 GMT  
 Long = Integer * Integer Overflow error
Bob,

Even though c is declared as long, VB is still expecting an integer on the
right side before it makes the assignment to c.  When you multiply A and B,
it expects an integer because integer * integer = integer.  To get around
this, you have to explicitly cast one of them to a long integer:

c = clng(a) * b

long * integer = long

Bich Leu


Quote:
> The following produces an overflow error if c is greater
> than 32,767.  It appears as if the long data type is being
> changed to an Integer.  Why?
> Dim a As Integer
> Dim b As Integer
> Dim c As Long
> a = 365
> b = 90
> c = a * b



Sat, 25 Sep 2004 05:56:47 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Converting integers and long integers to byte arrays

2. Overflow exceptions when converting from Long to Integer?

3. Is there a limit on a integer variable (overflow error)

4. Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

5. Integer VB Vrs Integer SQL Arrrgggggggg

6. Integer or "integer"??

7. Integer overflow traps

8. unsigned 32 bits integer arithmetic / overflow

9. ATT: Math wizards. Bit manipulations and integer overflow

10. HELP: Integer -> Hex conversion (Overflow)

11. Date as long integer

12. Passing signed Long Integers to VB 6 From C++

 

 
Powered by phpBB® Forum Software