Internal Representation of Values in Variants

Ø Variant variables maintain an internal representation of the values that they store. This representation determines how Visual Basic treats these values when performing comparisons and other operations.

Ø When you assign a value to a Variant variable, Visual Basic uses the most compact representation that accurately records the value. Later operations may cause Visual Basic to change the representation it is using for a particular variable. (A Variant variable is not a variable with no type; rather, it is a variable that can freely change its type.) These internal representations correspond to the explicit data types .

Ø A variant always takes up 16 bytes, no matter what you store in it. Objects, strings, and arrays are not physically stored in the Variant; in these cases, four bytes of the Variant are used to hold either an object reference, or a pointer to the string or array. The actual data are stored elsewhere.

Now you will see how different Datatypes are represented in Variant variables

Numeric Values Stored in Variant

Ø When you store whole numbers in Variant variables, Visual Basic uses the most compact representation possible.

Ø For example, if you store a small number without a decimal fraction, the Variant uses an Integer representation for the value.

Ø If you then assign a larger number, Visual Basic will use a Long value or, if it is very large or has a fractional component, a Double value.

Strings Stored in Variants

Ø Generally, storing and using strings in Variant variables poses few problems.

Ø As mentioned earlier, however, sometimes the result of the + operator can be ambiguous when used with two Variant values.

Ø If both of the Variants contain numbers, the + operator performs addition.

Ø If both of the Variants contain strings, then the + operator performs string concatenation.

Ø But if one of the values is represented as a number and the other is represented as a string, the situation becomes more complicated. Visual Basic first attempts to convert the string into a number. If the conversion is successful, the + operator adds the two values; if unsuccessful, it generates a Type mismatch error.

Ø To make sure that concatenation occurs, regardless of the representation of the value in the variables, use the & operator. For example, the following code:

Sub Form_Click ()

Dim X, Y

X = "6"

Y = "7"

Print X + Y, X & Y

X = 6

Print X + Y, X & Y

End Sub

produces this result on the form:

67 67

13 67

Visual Basic stores strings internally as Unicode.

Date/Time Values Stored in Variants

Ø Variant variables can also contain date/time values.

Ø You can also perform math on date/time values. Adding or subtracting integers adds or subtracts days; adding or subtracting fractions adds or subtracts time. Therefore, adding 20 adds 20 days, while subtracting 1/24 subtracts one hour.

Ø You can use date/time literals in your code by enclosing them with the number sign (#), in the same way you enclose string literals with double quotation marks (""). For example, you can compare a Variant containing a date/time value with a literal date:

If SomeDate > #3/6/93# Then

Similarly, you can compare a date/time value with a complete date/time literal:

If SomeDate > #3/6/93 1:20pm# Then

Objects Stored in Variants

Ø Objects can be stored in Variant variables.

Ø This can be useful when you need to gracefully handle a variety of data types, including objects.

Ø For example, all the elements in an array must have the same data type. Setting the data type of an array to Variant allows you to store objects alongside other data types in an array.

No comments:

Post a Comment