Редактиране

Споделяне чрез


Constant and Literal Data Types (Visual Basic)

A literal is a value that is expressed as itself rather than as a variable's value or the result of an expression, such as the number 3 or the string "Hello". A constant is a meaningful name that takes the place of a literal and retains this same value throughout the program, as opposed to a variable, whose value may change.

When Option Infer is Off and Option Strict is On, you must declare all constants explicitly with a data type. In the following example, the data type of MyByte is explicitly declared as data type Byte:

Option Strict On

Public Class Sample
  Public Const MyByte As Byte = 2
End Class

When Option Infer is On or Option Strict is Off, you can declare a constant without specifying a data type with an As clause. The compiler determines the type of the constant from the type of the expression. A numeric integer literal is cast by default to the Integer data type. The default data type for floating-point numbers is Double, and the keywords True and False specify a Boolean constant.

Literals and Type Coercion

In some cases, you might want to force a literal to a particular data type; for example, when assigning a particularly large integral literal value to a variable of type Decimal. The following example produces an error:

Dim myDecimal as Decimal  
myDecimal = 100000000000000000000   ' This causes a compiler error.  

The error results from the representation of the literal. The Decimal data type can hold a value this large, but the literal is implicitly represented as a Long, which cannot.

You can coerce a literal to a particular data type in two ways: by appending a type character to it, or by placing it within enclosing characters. A type character or enclosing characters must immediately precede and/or follow the literal, with no intervening space or characters of any kind.

To make the previous example work, you can append the D type character to the literal, which causes it to be represented as a Decimal:

Dim MyDecimal As Decimal = 100000000000000000000D

The following example demonstrates correct usage of type characters and enclosing characters:

' Default to Integer.
Public Const DefaultInteger As Integer = 100

' Default to Double.
Public Const DefaultDouble As Double = 54.3345612

' Force constant to be type Char.
Public Const MyCharacter As Char = "a"c

' DateTime constants.
Public Const MyDate As DateTime = #1/15/2001#
Public Const MyTime As DateTime = #1:15:59 AM#

' Force data type to be Long.
Public Const MyLong As Long = 45L

' Force data type to be Single.
Public Const MySingle As Single = 45.55!

The following table shows the enclosing characters and type characters available in Visual Basic.

Data type Enclosing character Appended type character
Boolean (none) (none)
Byte (none) (none)
Char " C
Date # (none)
Decimal (none) D or @
Double (none) R or #
Integer (none) I or %
Long (none) L or &
Short (none) S
Single (none) F or !
String " (none)

See also