VB Constants

Variables which do not change their value during the execution of a program are called constants. Let us revisit the definition of a variable, which is, 'a variable has a name and a value and stores values during program execution'. So, we can say that constants are a special kind of variable, having a name and a value but do not change the value during program execution.

For instance, if your program uses the name of the constant in your vb code.

Public Const PURCHASE_CAT = 'a'

Suppose the value of PURCHASE_CAT (Purchase transaction) appears many times in a program. Instead of using the value 'a' over and over again, we can define a constant and name it as PURCHASE_CAT, and use the name of the constant in the program, as shown below.


Once a constant is declared, we can't change its value in subsequent statements. So, its value specified in the constant's declaration will remain same in the entire program. Note that, constants are processed faster than variables, because the compiler substitutes constant names with their values. We could declare 'PURCHASE_CAT' as a variable, but constants are preferred for two reasons:

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

2.Constants are processed faster than variables. When a program is running, 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 we declare constants is similar to the manner in which we declare variables, except that in addition to supplying the constant's name, we must also supply a value, as follows:

Const constant name As type = value

Public Const PURCHASE_CAT As String= "a"

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

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. We can't use functions in declaring constants.

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. These constants correspond to integer values, but we don't need to know what these values are. We see only the names of the constants in the Properties window.

If we type the expression

CheckBoxl.CheckState =

a list of all possible values of the CheckState property will appear as soon as we type the equal sign, and we 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 we type. As a result, we don't have to memorize any of the constant names or look up their names. They're right there as we 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.

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

When developing an application, we can place all constants declarations in a single module. If we havemultiple modules which we use with several applications, try to include the module's name in the constant names to avoid conflicts and duplicate definitions.

In the title 'Database Programming using VB2005, C#, SQL Server 2005', we have shown how to place all the constants in a single module named constants.vb. This has facilitated placing of more than 20 constants pertaining to transaction type, group and account type in a single module, thus avoiding conflicts and duplicate definitions.