Constants


In many respects, a constant is a lot like a read-only variable. Both variable and constant declarations may have attributes, accessibility keywords, and initialization expressions. Both read-only variables and constants represent a value that the code cannot change after it is assigned.

The syntax for declaring a constant is as follows:

  [attribute_list] [accessibility] [Shadows] _ Const name [As type] = initialization_expression 

For the general meanings of the various parts of a constant declaration, see the section “Variable Declarations” earlier in this chapter. The following sections describe differences between read-only variable and constant declarations.

accessibility

When you declare a variable, you can omit the Dim keyword if you use any of the keywords Public, Protected, Friend, Protected Friend, Private, Static, or ReadOnly. You cannot omit the Const keyword when you declare a constant, because it tells Visual Basic that you are declaring a constant rather than a variable.

You cannot use the Static, ReadOnly, or Shared keywords in a constant declaration. Static implies that the value will change over time, and the value should be retained when the enclosing routine starts and stops. Because the code cannot change a constant’s value, that doesn’t make sense.

The ReadOnly keyword would be redundant because you already cannot change a constant’s value.

You use the Shared keyword in a variable declaration within a class to indicate that the variable’s value is shared by all instances of the class. If one object changes the value, all objects see the changed value. Because the program cannot change a constant’s value, the value need not be shared. All objects have the same version of the constant at all times. You can think of a constant as always shared.

You can use the other accessibility keywords in a constant declaration: Public, Protected, Friend, Protected Friend, and Private.

As type

If you have Option Strict turned on, you must include the constant’s data type. A constant can only be an intrinsic type (Boolean, Byte, Short, Integer, Long, Decimal, Single, Double, Char, String, Date, or Object) or the name of an enumerated type. You cannot declare a constant that is a class, structure, or array.

If you declare the constant with the Object data type, the initialization_expression must set the object equal to Nothing. If you want a constant that represents some other object, or a class, structure, or array, use a read-only variable instead.

Because the generic Object class doesn’t raise any events, and because you cannot make a constant of some other class type, it doesn’t make sense to use the WithEvents keyword in a constant declaration.

initialization_expression

The initialization_expression assigns the constant its never-changing value. You cannot use variables in the initialization_expression, but you can use conversion functions such as CInt. You can also use the values of previously defined constants and enumeration values. The expression can include type characters such as # or &H, and if the declaration doesn’t include a type statement (and Option Explicit is off), the type of the value determines the type of the constant.

The following code demonstrates these capabilities. The first statement uses the CInt function to convert the value 123.45 into an integer constant. The second and third statements set the values of two Long constants to hexadecimal values. The next statement combines the values defined in the previous two using a bitwise Or. The final statement sets a constant to a value defined by the enumerated type AccessLevel.

  Private Const MAX_VALUES As Integer = CInt(123.45) Private Const MASK_READ As Long = &H1000& Private Const MASK_WRITE As Long = &H2000& Private Const MASK_READ_WRITE As Long = MASK_READ Or MASK_WRITE Private Const MAX_ACCESS_LEVEL As AccessLevel = AccessLevel.SuperUser 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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