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
The syntax for declaring a constant is as
[ attribute_list ] [ accessibility ] [Shadows] _ Const
name[As type ] = initialization_expression
For the general meanings of the various
When you declare a variable, you can omit the
keyword if you use any of the keywords
. You cannot omit the
keyword when you declare a constant, because it
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
keyword in a variable declaration within a class to
You can use the other accessibility keywords in a constant declaration: Public , Protected , Friend , Protected Friend , and Private .
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.
The following code
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