The information in this article applies to:
SUMMARYWhen 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.
Some of these rules allow for simplified syntax when writing your code. For
example:
This piece of code takes advantage of the implicit Integer to Boolean coercion and correctly evaluates the expression in the If statement. 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 pervious 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:
The following scenario shows how implicit conversion can generate a variety
of unexpected results. You can add this code to a new program and run it
too see the output.
The output is:
Additional query words:
Keywords : kbVBp400 kbVBp500 kbVBp600 kbGrpVB |
Last Reviewed: January 5, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |