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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access