TYPE CHARACTERS

Data type characters identify a value’s data type. The following table lists the data type characters of Visual Basic.

CHARACTER DATA TYPE
% Integer
& Long
@ Decimal
! Single
# Double
$ String

You can specify a variable’s data type by adding a data type character after a variable’s name when you declare it. When you use the variable later, you can omit the data type character if you like. For example, the following code declares variable num_desserts as a Long and satisfaction_quotient as a Double. It then assigns values to these variables.

Dim num_desserts&
Dim satisfaction_quotient#
 
num_desserts = 100
satisfaction_quotient# = 1.23

If you have Option Explicit turned off, you can include a data type character the first time you use the variable to determine its data type. If you omit the character, Visual Basic picks a default data type based on the value you assign to the variable.

If the value you assign is an integral value that will fit in an Integer, Visual Basic makes the variable an Integer. If the value is too big for an Integer, Visual Basic makes the variable a Long. If the value contains a decimal point, Visual Basic makes the variable a Double.

If you set a variable equal to a True or False, Visual Basic makes it a Boolean.

In Visual Basic, you surround date values with # characters. If you assign a variable to a date value, Visual Basic gives the variable the Date data type. The following code assigns Boolean and Date variables:

a_boolean = True
a_date = #12/31/2007#

In addition to data type characters, Visual Basic provides a set of literal type characters that determine the data type of literal values. These are values that you explicitly type into your code in statements such as assignment and initialization statements. The following table lists the literal type characters of Visual Basic.

CHARACTER DATA TYPE
S Short
US UShort
I Integer
UI UInteger
L Long
UL ULong
D Decimal
F Single (F for floating point)
R Double (R for real)
c Char (note lowercase c)

A literal type character determines the data type of a literal value in your code and may indirectly determine the data type of a variable assigned to it. For example, suppose that the following code is the first use of the variables i and ch (with Option Explicit turned off):

i = 123L
ch = "X"c

Normally, Visual Basic would make i an Integer because the value 123 fits in an Integer. Because the literal value 123 ends with the L character, however, the value is a Long, so the variable i is also a Long.

Similarly, Visual Basic would normally make variable ch a String because the value "X" looks like a string. The c following the value tells Visual Basic to make this a Char variable instead.

Visual Basic also lets you precede a literal integer value with &H to indicate that it is hexadecimal (base 16) or &O to indicate that it is octal (base 8). For example, the following three statements set the variable flags to the same value. The first statement uses the decimal value 100, the second uses the hexadecimal value &H64, and the third uses the octal value &O144.

flags = 100   ' Decimal 100.
flags = &H64  ' Hexadecimal &H64 = 6 * 16 + 4 = 96 + 4 = 100.
flags = &O144 ' Octal &O144 = 1 * 64 + 4 * 8 + 4 = 64 + 32 + 4 = 100.

BASE CONVERSIONS
The Hex and Oct functions let you convert numeric values into hexadecimal and octal strings, respectively. In some sense, this is the opposite of what the &H and &O codes do: make Visual Basic interpret a string literal as hexadecimal or octal number.
The following code displays the value of the variable flags in decimal, hexadecimal, and octal:
Debug.WriteLine(flags)      ' Decimal.
Debug.WriteLine(Hex(flags)) ' Hexadecimal.
Debug.WriteLine(Oct(flags)) ' Octal.

Sometimes you must use literal type characters to make a value match a variable’s data type. For example, consider the following code:

Dim ch As Char
ch = "X"  ' Error because "X" is a String.
ch = "X"c ' Okay because "X"c is a Char.
 
Dim amount As Decimal
amount = 12.34  ' Error because 12.34 is a Double.
amount = 12.34D ' Okay because 12.34D is a Decimal.

The first assignment tries to assign the value "X" to a Char variable. This throws an error because "X" is a String value so it won’t fit in a Char variable. Although it is obvious to a programmer that this code is trying to assign the character X to the variable, Visual Basic thinks the types don’t match.

The second assignment statement works because it assigns the Char value "X"c to the variable. The next assignment fails when it tries to assign the Double value 12.34 to a Decimal variable. The final assignment works because the value 12.34D is a Decimal literal.

The following code shows another way to accomplish these assignments. This version uses the data type conversion functions CChar and CDec to convert the values into the proper data types. The following section, “Data Type Conversion,” has more to say about data type conversion functions.

ch = CChar("X")
amount = CDec(12.34)

Using data type characters, literal type characters, and the Visual Basic default data type assignments can lead to very confusing code. You cannot expect every programmer to notice that a particular variable is a Single because it is followed by ! in its first use but not in others. You can make your code less confusing by using variable declarations that include explicit data types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.92.199