2.3. Constants


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").

2.3.1. Literals

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 

2.3.2. Enumerations

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.

VB 6 Tip: Enumerations are not new in VB 2005; VB 6 programmers should already be familiar with enumerations.


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 

Note that if you turn Option Strict On, the second statement above should be:

 theWeek = CType(1, Week) 

You need to explicitly convert the Integer value to the enumeration. In "Type Conversion," later in this chapter, you will learn about the Option Strict statement in more detail.

If you do not explicitly perform the conversion, Visual Studio 2005 will underline the number 1. You can position your cursor under the number and click on the down arrow (see Figure 2-3). Visual Studio 2005 will suggest to you the remedy. This feature is known as AutoCorrect.


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 



Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

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