Previous | Index | Next 

[PRB] Math operations on Variants cause an Overflow exception

Under VB6, math operations on Variant variables automatically promote the value to a type with higher capacity if the operation is about to produce an overflow. For example, the following VB6 code avoids the overflow by promoting its arguments to Double:

Dim v As Variant
	v = 2000000000   ' this is a 32-bit integer
	v = v + v        ' result is a Double value

This behavior isn’t supported by the VB6Variant type defined in the CodeArchitects.VBLibrary DLL. All math operations are performed using the capacity inferred by the operand with the higher capacity; if adopted capacity isn’t big enough for the result, an OverflowException occur.

The only workaround for this issue is forcing the promotion of either argument to a larger capacity, as in this example:

Dim v As Variant
	v = 2000000000 
	v = v + CDbl(v)

 

Previous | Index | Next