Type Characters (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.

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

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 characterData typeExample
%IntegerDim L%
&LongDim M&
@DecimalConst W@ = 37.5
!SingleDim Q!
#DoubleDim X#
$StringDim V$ = "Secret"

No identifier type characters exist for the Boolean, Byte, Char, Date, Object, SByte, Short, UInteger, ULong, or UShort data types, or for any composite data types such as arrays or structures.

In some cases, you can append the $ character to a Visual Basic function, for example Left$ instead of Left, to obtain a returned value of type String.

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

A literal is a textual representation of a particular value of a data type.

Default Literal Types

The form of a literal as it appears in your code ordinarily determines its data type. The following table shows these default types.

Textual form of literalDefault data typeExample
Numeric, no fractional partInteger2147483647
Numeric, no fractional part, too large for IntegerLong2147483648
Numeric, fractional partDouble1.2
Enclosed in double quotation marksString"A"
Enclosed within number signsDate#5/17/1993 9:32 AM#

Forced Literal Types

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 characterData typeExample
SShortI = 347S
IIntegerJ = 347I
LLongK = 347L
DDecimalX = 347D
FSingleY = 347F
RDoubleZ = 347R
USUShortL = 347US
UIUIntegerM = 347UI
ULULongN = 347UL
CCharQ = "."C

No literal type characters exist for the Boolean, Byte, Date, Object, SByte, or String data types, or for any composite data types such as arrays or structures.

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

The compiler normally construes an integer literal to be in the decimal (base 10) number system. You can force an integer literal to be hexadecimal (base 16) with the &H prefix, and you can force it to be octal (base 8) with the &O prefix. The digits that follow the prefix must be appropriate for the number system. The following table illustrates this.

Number basePrefixValid digit valuesExample
Hexadecimal (base 16)&H0-9 and A-F&HFFFF
Octal (base 8)&O0-7&O77

You can follow a prefixed literal with a literal type character. The following example shows this.

Dim counter As Short = &H8000S  
Dim flags As UShort = &H8000US  

In the previous example, counter has the decimal value of -32768, and flags has the decimal value of +32768.

Data Types
Elementary Data Types
Value Types and Reference Types
Type Conversions in Visual Basic
Troubleshooting Data Types
Variable Declaration
Data Types

Show: