Numeric Literals String Literals Character Literals

28 Literals have data types just as variables do. The 10 in this code fragment is interpreted by the compiler as type Integer because it is an integer that falls within the range of the Integer type.

2.4.1 Numeric Literals

Any integer literal that is within the range of the Integer type -2147483648 through 2147483647 is interpreted as type Integer, even if the value is small enough to be interpreted as type Byte or Short. Integer literals that are outside the Integer range but are within the range of the Long type - 9223372036854775808 through 9223372036854775807 are interpreted as type Long. Integer literals outside the Long range cause a compile-time error. Numeric literals can also be of one of the floating point types—Single, Double, and Decimal. For example, in this line of code, 3.14 is a literal of type Double: z = y 3.14 In the absence of an explicit indication of type discussed shortly, Visual Basic .NET interprets floating point literals as type Double. If the literal is outside the range of the Double type - 1.7976931348623157E308 through 1.7976931348623157E308, a compile-time error occurs. Visual Basic .NET allows programmers to explicitly specify the types of literals. Table 2- 2 shown later in this chapter lists Visual Basic .NETs intrinsic data types, along with the method for explicitly defining a literal of each type. Note that for some intrinsic types, there is no way to write a literal.

2.4.2 String Literals

Literals of type String consist of characters enclosed within quotation-mark characters. For example, in the following line of code, hello, world is a literal of type String: Console.WriteLinehello, world String literals are not permitted to span multiple source lines. In other words, this is not permitted: Wrong Console.WriteLinehello, world To write a string literal containing quotation-mark characters, type the character twice for each time it should appear. For example: Console.WriteLineSo then Dave said, hello, world. This line produces the following output: So then Dave said, hello, world.

2.4.3 Character Literals

Visual Basic .NETs Char type represents a single character. This is not the same as a one-character string; Strings and Chars are distinct types. Literals of type Char consist of a single character enclosed within quotation-mark characters, followed by the character c . For example, in the following code, Ac is a literal of type Char: Dim MyChar As Char MyChar = Ac 29 To emphasize that this literal is of a different data type than a single-character string, note that this code causes a compile-time error if Option Strict is On : Wrong Dim MyChar As Char MyChar = A The error is: Option Strict On disallows implicit conversions from String to Char.

2.4.4 Date Literals