PRB: Run-Time Overflow Error w/Byte Type Counter in For Loop
Article ID: 129709
Article Last Modified on 12/9/2003
APPLIES TO
- Microsoft Visual Basic 4.0 Standard Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 16-bit Enterprise Edition
- Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
This article was previously published under Q129709
SYMPTOMS
Using a byte variable as a counter in a For loop with a negative Step value
causes an Overflow error at run-time.
CAUSE
In a For loop in Visual Basic for Windows, the Start, Limit and Step values
are all coerced to the type of the For loop counter. Since the byte data
type cannot be negative, it cannot have a negative step value.
RESOLUTION
This is a limitation of the implementation of the byte datatype and For
loop. The solution is to never use byte type variables in For loops where
the step is negative or a variable (since the variable may contain a
negative number at some future time.)
STATUS
This behavior is by design.
Additional query words: increment decrement counter
Keywords: kbprogramming kbprb KB129709