Syntax [accessModifier] Const [Shadows] name [As type] = value
accessModifier (optional) Specifies the scope and accessibility of the constant. One of the following access levels: Access level | Description |
---|
Public | The constant is publicly accessible anywhere, both inside and outside of the project. | Private | The constant is accessible only within the defining type. | Protected | The constant is accessible only to the code in the defining type or to one of its derived types. | Friend | The constant is accessible only within the project that contains the constant definition. | Protected Friend | Combines the access features of Protected and Friend. |
If omitted, the Public access level is used. The access modifier is excluded when the Const statement is used within a procedure.
Shadows (optional) Indicates that the constant shadows an identically named element in a base class. The Shadows keyword is excluded when the Const statement is used within a procedure.
name (required) The name of the constant.
type (optional; Type) The data type of the constant; it can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, UShort, or the name of any enumerated type.
value (required; any) A literal, constant, or an expression made up of literals, constants, and the arithmetic or logical operators, except Is and IsNot. Any of the conversion functions (such as CByte) are allowed, as is AscW. Description The Const statement defines a constant value within a class, structure, or procedure. These constants are also referred to as symbolic constants. Usage at a Glance If Option Strict is On, the As type clause is required. Constants are inherently shared, static, and read-only. Multiple constants can be defined through the same Const statement by separating the definitions with commas. Each definition requires its own As clause. If you are building a large application with many different modules, you may find your code easier to maintain if you create a single separate code module to hold your Public constants. If two or more constants with integral values make up a set of constants, you should consider defining them through an enumerated data type using the Enum statement. Example Private Const MY_CONSTANT As Double = 3.1417# See Also Dim Statement, Enum Statement |