Type Characters


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

Open table as spreadsheet

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.

The following code shows the first use of three variables (Option Explicit is off). The first statement sets the variable an_integer equal to the value 100. This value fits in an Integer, so Visual Basic makes the variable an Integer. The second statement sets a_long equal to 10000000000. That value is too big to fit in an Integer, so Visual Basic makes it a Long. The third statement sets a_double to 1.0. That value contains a decimal point, so Visual Basic makes the variable a Double.

  an_integer = 100 a_long = 10000000000 a_double = 1.0 

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

Dates in Visual Basic are delimited 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.

Open table as spreadsheet

Character

Data Type

S

Short

US

UShort

I

Integer

UI

UInteger

L

Long

IL

ULong

D

Decimal

F

Single (F for floating point)

R

Double (R for real)

c

Char (note lower case 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). 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, as shown here:

  i = 123L ch = "X"c 

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 * 8 * 8 + 4 * 8 + 4 = 64 + 32 + 4 = 100. 

As an aside, note that 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. The following example 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, the first assignment in the following code tries to assign the value “X” to a Char variable. This throws an error because “X” is a String value. 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.

  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 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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net