Article ID: 129803 - Last Review: May 6, 2003 - Revision: 2.0 INFO: Variable Coercion Rules in Visual BasicThis article was previously published under Q129803 On This PageSUMMARY
When two or more variables of different types are involved in an
expression, Visual Basic uses a set of internal type coercion rules to
change the different types into a single type. For every combination of
input and result types, there is a specific coercion rule. (Contrary to
some speculation, Variants are not involved.) This makes Visual Basic
faster and more efficient, but it can produce some puzzling results if you
aren't aware of what Visual Basic is doing behind the scenes. This article
lays out some of the less obvious conversion rules, their benefits, and
some possible scenarios where this can lead to unexpected results.
MORE INFORMATION
Coercion between numeric types is pretty straightforward. Smaller types
(integer) coerced to larger types (double) simply have their value
assigned. Larger types (double) coerced to smaller types (integer) have
their value assigned, but generate an Overflow error if the smaller type
cannot contain the larger type's value. Other coercion rules are not as
obvious. NOTE: Any type can be implicitly coerced to any other type (excluding objects).
Source Type Coerced to Apply this rule
--------------------------------------------------------------
Integer Boolean 0=False, non-zero=True
Boolean Byte False=0, True=255
Boolean any numeric False=0, True=-1
(except Byte)
String Date String is analyzed for mm/dd/yy,
and so on
Date numeric type Coerce to Double and use
DateSerial(Double)
numeric type Date Use number as serial date, check
valid date range
numeric type Byte Error if negative
String numeric type Strings are treated as a Double
when they need to represent a
number
ReturnCode% = FunctionThatReturnsAnInteger() If (ReturnCode%) Then ... Note on Strings as NumbersTreating Strings as Doubles when they need to represent a number gives the String the maximum possible range and nearly the best possible precision (only Currency can have more precision, at a sacrifice in range). Because these coercions are generated by the compiler, the rule must be decided up front without regard to the actual content of the string.Note that "treating as Double" is not quite the same as "coercing to Double." For example, addition and subtraction operators treat Currency as a preferable type to Double. Currency plus String will be treated as Currency plus Double, which would use Currency addition. Thus the string will be coerced directly to Currency. Why Use Coercion Rules?The largest reason is performance. Hard-coded coercion rules make Visual Basic version 4.0 faster and more efficient and provide backward compatibility to previous versions of Visual Basic. One of the big speed advantages comes because Visual Basic version 4.0 now knows the data type of control properties. Knowing the type of properties provides trememdous performance advantages. Without it, for example, setting a property required Basic to package the value in a Variant, and the control to unpack and coerce it to the right type. Now the control knows it is receiving the correct specific type, and Basic does a direct coercion to that type without ever involving the overhead of a Variant.Unexpected ResultsThe following code pieces show a few common scenarios where errors can be generated by Visual Basic if types and coercion rules are not carefully considered:a = 1 String b = 23 String a + b = 123 String a + b - 1 = 122 Double 1 - a + b = 23 Double a + b + 1 = 124 Double 1 + a + b = 25 Double (1 + b) / a = 24 Double APPLIES TO
| Article Translations
|
Back to the top
