Export (0) Print
Expand All

Type Characters

In addition to specifying a data type in a declaration statement, you can force the data type of some programming elements with a type character. The type character must immediately follow the element name, with no intervening characters of any kind.

The type character is not part of the name of the element. An element defined with a type character can be referenced without the type character.

Identifier Type Characters

Visual Basic supplies a set of identifier type characters, which you can use in a declaration to specify the data type of a variable or constant. The following table shows the available identifier type characters with examples of usage.

Identifier type character Data type Example
% Integer
Dim L% 
& Long
Public M& 
@ Decimal
Const W@ = 37.5 
! Single
Dim Q! 
# Double
Static X# 
$ String
Private V$ = "Secret" 

No identifier type characters exist for the Boolean, Byte, Char, Date, Object, or Short data types, or for any composite data types.

Identifier type characters also can force the data type of other identifiers, and you can even use them in expressions. In some cases, you can append the $ character to a Visual Basic function, for example Left$ instead of Left.

In all cases, the identifier type character must immediately follow the identifier.

Literal Type Characters

A literal is a textual representation of a particular value of a type. The form of a literal as it appears in your code usually determines its data type. The compiler treats integral literals as Integer unless they are large enough to be Long, and it treats nonintegral literals as Double. In the following statements, the data type of the literal is not ambiguous:

J = 347    ' This literal is of the Integer data type.
X = 9.2    ' This literal is of the Double data type.
B = False  ' This literal is of the Boolean data type.

Visual Basic supplies a set of literal type characters, which you can use to force a literal to assume a data type other than the one its form indicates. You do this by appending the character to the end of the literal. The following table shows the available literal type characters with examples of usage.

Literal type character Data type Example
S Short
I = 347S 
I Integer
J = 347I 
L Long
K = 347L 
D Decimal
H = 347D 
F Single
Y = 347F 
R Double
Z = 347R 
C Char
Q = "."C 

No literal type characters exist for the Boolean, Byte, Date, Object, or String data types, or for any composite data types.

Literals can also use the identifier type characters (%, &, @, !, #, $), as can variables, constants, and expressions. However, the literal type characters (S, I, L, D, F, R, C) can be used only with literals.

In all cases, the literal type character must immediately follow the literal.

See Also

Constant and Literal Data Types

Show:
© 2014 Microsoft