Export (0) Print
Expand All

Const Statement

Used at module, class, structure, procedure, or block level to declare constants for use in place of literal values.

[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend | 
Private }] [ Shadows ] Const name [ As type ] = initexpr

Parts

attrlist
Optional. List of attributes that apply to the constants declared in this statement. Multiple attributes are separated by commas.
Public
Optional. Constants declared with the Public keyword have public access. There are no restrictions on the accessibility of public constants.
Protected
Optional. Constants declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Constants declared with the Friend keyword have friend access. They are accessible from within their declaration context and from anywhere else in the same program.
Protected Friend
Optional. Constants declared with the Protected Friend keywords have the union of protected and friend access. They can be used by code in the same program, as well as by code in derived classes. Protected friend access can be specified only on members of classes.
Private
Optional. Constants declared with the Private keyword have private access. They are accessible only from within their declaration context, including from members of any nested types such as procedures.
Shadows
Optional. Indicates that this constant shadows an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable from within the derived class that shadows it, unless the shadowing element is inaccessible, for example if it is Private.
name
Required. Name of the constant. Must be a valid Visual Basic identifier. You can declare as many constants as you like in the same declaration statement, specifying the name and initexpr parts for each one. Multiple constants are separated by commas.
type
Optional unless Option Strict is On. Data type of the constant. Can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, or the name of an enumeration. You must use a separate As clause for each constant being defined.

You cannot use variables or functions in initexpr. However, you can use the conversion keywords such as CInt and CDate, and you can use the AscW and ChrW methods of the Strings class in the Microsoft.VisualBasic namespace.

If you do not specify type, the constant takes the data type of initexpr. If you do specify type, the data type of initexpr must be convertible to type.

If type is Object, initexpr must be Nothing.

initexpr
Required. Expression that is evaluated and assigned to the constant when it is created. Can consist of a literal; another constant that is already defined; a member of an enumeration; or any combination of literals, constants, and enumeration members. You can use arithmetic and logical operators to combine such elements.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

Remarks

Constants declared in a Sub, Function, or Property procedure are local to that procedure. A constant declared outside a procedure is defined throughout the class, module, or structure in which it is declared.

You can use a constant anywhere you can use an expression.

Example

This example uses the Const statement to declare constants for use in place of literal values.

' Constants are shared by default.
Const MyVar As Integer = 459
' Declare Public String constant -- valid only at class or module level.
Public Const MyString As String = "HELP"
' Declare Private Integer constant -- valid only at class or module level.
Private Const MyInt As Integer = 5

See Also

Enum Statement | #Const Directive | Dim Statement | ReDim Statement

Show:
© 2014 Microsoft