Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

120 Chapter 3 VISUAL BASIC: THE LANGUAGE

VB6 VB.NET

In VB6 you could declare all the variables in a procedure as static by prefixing the procedure definition with the keyword Static. This option is no longer available with VB.NET: the Static modifier is not a valid modifier for procedures.

Variables declared in a module outside any procedure take effect when the form is loaded and cease to exist when the form is unloaded. If the form is loaded again, its variables are initialized, as if it’s being loaded for the first time.

Variables are initialized when they’re declared, according to their type. Numeric variables are initialized to zero, string variables are initialized to a blank string, and Object variables are initialized to Nothing. Of course, if the variable is declared with an initializer (as in Dim last As Integer = 99), it is initialized to the specified value.

Constants

Some variables don’t change value during the execution of a program. These are constants that appear many times in your code. For instance, if your program does math calculations, the value of pi (3.14159…) may appear many times. Instead of typing the value 3.14159 over and over again, you can define a constant, name it pi, and use the name of the constant in your code. The statement

circumference = 2 * pi * radius

is much easier to understand than the equivalent

circumference = 2 * 3.14159 * radius

You could declare pi as a variable, but constants are preferred for two reasons:

Constants don’t change value. This is a safety feature. Once a constant has been declared, you can’t change its value in subsequent statements, so you can be sure that the value specified in the constant’s declaration will take effect in the entire program.

Constants are processed faster than variables. When the program is running, the values of constants don’t have to be looked up. The compiler substitutes constant names with their values, and the program executes faster.

The manner in which you declare constants is similar to the manner in which you declare variables, except that in addition to supplying the constant’s name, you must also supply a value, as follows:

Const constantname As type = value

Constants also have a scope and can be Public or Private. The constant pi, for instance, is usually declared in a module as Public so that every procedure can access it:

Public Const pi As Double = 3.14159265358979

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

CONSTANTS 121

The name of the constant follows the same rules as variable names. The constant’s value is a literal value or a simple expression composed of numeric or string constants and operators. You can’t use functions in declaring constants. The best way to define the value of the pi variable is to use the pi member of the Math class:

pi = Math.pi

However, you can’t use this assignment in the constant declaration. You must supply the actual value.

Constants can be strings, too, like these:

Const ExpDate = #31/12/1997#

Const ValidKey = “A567dfe”

Visual Basic uses constants extensively to define method arguments and control properties. The value of a CheckBox control, for instance, can be CheckState.Checked or CheckState.UnChecked. If the CheckBox control’s ThreeState property is True, it can have yet another value, which is CheckState.Intederminate. These constants correspond to integer values, but you don’t need to know what these values are. You see only the names of the constants in the Properties window. If you type the expression

CheckBox1.CheckState =

a list of all possible values of the CheckState property will appear as soon as you type the equal sign, and you can select one from the list.

VB.NET recognizes numerous constants, which are grouped according to the property they apply to. Each property’s possible values form an enumeration, and the editor knows which enumeration applies to each property as you type. As a result, you don’t have to memorize any of the constant names or look up their names. They’re right there as you type, and their names make them self-explanatory. Notice that the name of the constant is prefixed by the name of the enumeration it belongs to.

Note Enumerations are often named after the property they apply to, but not always. The set of possible values of the BorderStyle property for all controls is named BorderStyle enumeration. The value set for the alignment of the text on a control, however, is the HorizontalAlignment enumeration. But you always see the proper enumeration in the Properties window, and the editor knows which one to display and when.

Constant declarations may include other constants. In math calculations, the value 2 × pi is almost as common as the value pi. You can declare these two values as constants:

Public Const pi As Double = 3.14159265358979

Public Const pi2 As Double = 2 * pi

Tip When defining constants in terms of other constants, especially if they reside in different modules, be sure to avoid circular definitions. Try to place all your constant declarations in the same module. If you have modules you use with several applications, try to include the module’s name in the constant names to avoid conflicts and duplicate definitions.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com