VBA overflow error with byte variables -
can please explain why following code generate overflow error in vba when recipient of operation c
integer
?
dim byte, b byte dim c integer = 3: b = 100 c = * b
or mean every operation involving 'byte` variables have yield result between 0 , 255 regardless of recipient variable type?
or mean every operation involving byte variables have yield result between 0 , 255 regardless of recipient variable type
yes, because bytes hold values 0 255, multiplying 3 x 100, passing (overflowing) capacity, though afterwards passing result integer.
because multiplying 2 bytes together, vba assumes result byte too. after calculation result then cast integer.
to around this, must cast @ least 1 of variables. lets vba know must make room larger result:
dim byte, b byte dim c integer = 3 b = 100 c = * cint(b) ' <-- cast b integer prevent overflow error
Comments
Post a Comment