Literals


The quickest way to include values of a particular data type in your Visual Basic code is to use a literal. You've already seen literals in action in this book. Chapter 1 included a literal in its sample project.

MsgBox("Hello, World!") 


This call to the MsgBox function includes a String literal. String literals always appear within a set of double quotes. Most numeric literals appear with a data-type-defining character on the end of the literal, but there are other variations. Table 6-6 lists the different literals values you can include in your code.

Table 6-6. Literals Supported by Visual Basic

Literal Type

Example

Description

Boolean

true

The Boolean data type supports two literal values: True and False.

Char

"Q"c

Single-character literals appear in double quotes with a trailing character c. A literal of type Char is not the same as a single-character literal of type String.

Date

#11/7/2005#

Date or time literals appear between a set of number signs. You can include dates, times, or a combination of both. The date or time values can be in any format recognized by Windows, although Visual Studio may reformat your date literal for conformity with its own standards.

Decimal

123.45D

123.45@

Floating point values of type Decimal are followed by a capital D, or the character @.

Double

123.45R

123.45#

Floating point values of type Double are followed by a capital R, or the character #. Also, if you use a numeric literal with a decimal portion, but with no trailing data type character, that literal will be typed as a Double.

Hexadecimal

&HABCD

You can include hexadecimal literals in your code by starting the value with the "&H" character sequence, followed by the hex digits.

Integer

123.45I

123.45%

Integral values of type Integer are followed by a capital I, or the character %. Also, if you use a numeric literal that falls in the range of an Integer, but with no trailing data type character, that literal will be typed as an Integer.

Long

123.45L

123.45&

Integral values of type Long are followed by a capital L, or the character &. Also, if you use a numeric literal that falls in the range of a Long and outside the range of an Integer, but with no trailing data type character, that literal will be typed as a Long.

Octal

&O7654

You can include octal literals in your code by starting the value with the "&O" character sequence, followed by the octal digits.

Short

123.45S

Integral values of type Short are followed by a capital S.

Single

123.45F

123.45!

Floating point values of type Single are followed by a capital F, or the character !.

String

"Simple"

"A ""B"" C"

String literals appear within a set of double quotes, with no special character following the closing quote. Use two quote characters within the string literal to embed a single quotation mark.





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

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