While variables are a powerful tool, there are times when you want their values to remain constant. Perhaps your program makes repeated use of the value of pi or the natural logarithm e. A constant is like a variable in that it can store a value; unlike a variable, the value of a constant cannot be changed while the program runs. You declare constants using the Const keyword. The following definition assigns the value 3.14 to a constant whose name is pi and then uses it in calculating the area of a circle:
Const pi As Double = 3.14 Dim radius as Double = 5 Dim area as Double = pi * radius ^ 2
A constant of this type is sometimes called a symbolic constant, because it uses a word to represent a value. VB 2005 supports two additional kinds of constants: literals (see "Literals") and enumerations, or enums (See "Enumerations").
As in VB 6, a literal, or literal constant, as it is sometimes called, represents a particular value in text. For example, the number 32, as it appears in this sentence, is a literal constant. The value of 32 is always 32. Likewise, a quoted string like "Hello World" is also a literal constant. Literal types include Booleans, integers, floating-point numbers, strings, characters, and dates. Any number that is within the range of Integer types, such as 32, is an Integer type by default.
For example, the following statements assign the literal A to ch1 and ch2, both of which are Char types:
'---assign the character "A" to ch1 and ch2 Dim ch1 As Char = "A"c Dim ch2 As Char = Chr(65) Dim longValue as Long = 100L
To represent the quotation character (") in a string variable, use the quotation character twice in succession, as in the following snippet:
Dim str As String ' assigns str to "He said: "VB is so cool!"" str = "He said: ""VB is so cool!"""
To assign a date and time to a DateTime variable, use the # character:
Dim timeNow As DateTime timeNow = #3/22/2005 10:01:19 AM#
To represent a large number, you can use the exponent symbol (E) to separate its mantissa (the significant digits; 3.8896, in the example below) and its exponent (a power of 10; 23, in this case):
Dim f As Double f = 3.8896E+23
Sometimes it is easier to work with named constants than with numeric constants. Enumerations provide a powerful tool for creating logically related collections of named constants, such as the names of the primary colors, or the days of the week. For example, you might wish to represent the days of the week with numbers, such as 1 for Monday, 2 for Tuesday, and so on.
But when it comes to writing a program, it will likely be more intuitive to use the names of the days instead. You can do so by declaring an enumeration that associates each day of the week with a number.
The following shows the Week enumeration:
Enum Week Sunday = 0 Monday = 1 Tuesday = 2 Wednesday = 3 Thursday = 4 Friday = 5 Saturday = 6 End Enum
To use the enumeration, declare a variable of type Week:
Dim theWeek As Week
You can now assign the day of a week using a named constant:
theWeek = Week.Monday ' or theWeek = 1 ' both are equivalent
To print out the month, you can use either of the following:
MsgBox(theWeek) ' prints out 1 MsgBox(theWeek.ToString) ' prints out Monday
Besides defining your own enumerations, there are also predefined enumerations with which you might already be familiar. For example, the result from the MsgBox function is an enumeration called MsgBoxResult:
Figure 2-3. AutoCorrect in Visual Studio 2005
Dim response As MsgBoxResult response = MsgBox("Are you sure?", MsgBoxStyle.YesNo) If response = MsgBoxResult.Yes Then ' do something Else ' do something End If