|
1 | 1 | ---
|
2 | 2 | title: "Const Statement (Visual Basic)"
|
3 | 3 | ms.date: 05/12/2018
|
4 |
| -f1_keywords: |
| 4 | +f1_keywords: |
5 | 5 | - "vb.Const"
|
6 |
| -helpviewer_keywords: |
| 6 | +helpviewer_keywords: |
7 | 7 | - "Const statement [Visual Basic]"
|
8 | 8 | ms.assetid: 495b318d-b7c5-4198-94f8-0790a541b07a
|
9 | 9 | ---
|
10 | 10 | # Const Statement (Visual Basic)
|
11 |
| -Declares and defines one or more constants. |
12 |
| - |
13 |
| -## Syntax |
14 |
| - |
15 |
| -```vb |
16 |
| -[ <attributelist> ] [ accessmodifier ] [ Shadows ] |
17 |
| -Const constantlist |
18 |
| -``` |
19 |
| - |
20 |
| -## Parts |
21 |
| - `attributelist` |
22 |
| - Optional. List of attributes that apply to all the constants declared in this statement. See [Attribute List](../../../visual-basic/language-reference/statements/attribute-list.md) in angle brackets ("`<`" and "`>`"). |
23 |
| - |
24 |
| - `accessmodifier` |
25 |
| - Optional. Use this to specify what code can access these constants. Can be [Public](../../../visual-basic/language-reference/modifiers/public.md), [Protected](../../../visual-basic/language-reference/modifiers/protected.md), [Friend](../../../visual-basic/language-reference/modifiers/friend.md), [Protected Friend](../modifiers/protected-friend.md), [Private](../../../visual-basic/language-reference/modifiers/private.md), or [Private Protected](../../language-reference/modifiers/private-protected.md). |
26 |
| - |
27 |
| - `Shadows` |
28 |
| - Optional. Use this to redeclare and hide a programming element in a base class. See [Shadows](../../../visual-basic/language-reference/modifiers/shadows.md). |
29 |
| - |
30 |
| - `constantlist` |
31 |
| - Required. List of constants being declared in this statement. |
32 |
| - |
33 |
| - `constant` `[ ,` `constant` `... ]` |
34 |
| - |
35 |
| - Each `constant` has the following syntax and parts: |
36 |
| - |
37 |
| - `constantname` `[ As` `datatype` `] =` `initializer` |
38 |
| - |
39 |
| -|Part|Description| |
40 |
| -|----------|-----------------| |
41 |
| -|`constantname`|Required. Name of the constant. See [Declared Element Names](../../../visual-basic/programming-guide/language-features/declared-elements/declared-element-names.md).| |
42 |
| -|`datatype`|Required if `Option Strict` is `On`. Data type of the constant.| |
43 |
| -|`initializer`|Required. Expression that is evaluated at compile time and assigned to the constant.| |
44 |
| - |
45 |
| -## Remarks |
46 |
| - If you have a value that never changes in your application, you can define a named constant and use it in place of a literal value. A name is easier to remember than a value. You can define the constant just once and use it in many places in your code. If in a later version you need to redefine the value, the `Const` statement is the only place you need to make a change. |
47 |
| - |
48 |
| - You can use `Const` only at module or procedure level. This means the *declaration context* for a variable must be a class, structure, module, procedure, or block, and cannot be a source file, namespace, or interface. For more information, see [Declaration Contexts and Default Access Levels](../../../visual-basic/language-reference/statements/declaration-contexts-and-default-access-levels.md). |
49 |
| - |
50 |
| - Local constants (inside a procedure) default to public access, and you cannot use any access modifiers on them. Class and module member constants (outside any procedure) default to private access, and structure member constants default to public access. You can adjust their access levels with the access modifiers. |
51 |
| - |
52 |
| -## Rules |
53 |
| - |
54 |
| -- **Declaration Context.** A constant declared at module level, outside any procedure, is a *member constant*; it is a member of the class, structure, or module that declares it. |
55 |
| - |
56 |
| - A constant declared at procedure level is a *local constant*; it is local to the procedure or block that declares it. |
57 |
| - |
58 |
| -- **Attributes.** You can apply attributes only to member constants, not to local constants. An attribute contributes information to the assembly's metadata, which is not meaningful for temporary storage such as local constants. |
59 |
| - |
60 |
| -- **Modifiers.** By default, all constants are `Shared`, `Static`, and `ReadOnly`. You cannot use any of these keywords when declaring a constant. |
61 |
| - |
62 |
| - At procedure level, you cannot use `Shadows` or any access modifiers to declare local constants. |
63 |
| - |
64 |
| -- **Multiple Constants.** You can declare several constants in the same declaration statement, specifying the `constantname` part for each one. Multiple constants are separated by commas. |
65 |
| - |
66 |
| -## Data Type Rules |
67 |
| - |
68 |
| -- **Data Types.** The `Const` statement can declare the data type of a variable. You can specify any data type or the name of an enumeration. |
69 |
| - |
70 |
| -- **Default Type.** If you do not specify `datatype`, the constant takes the data type of `initializer`. If you specify both `datatype` and `initializer`, the data type of `initializer` must be convertible to `datatype`. If neither `datatype` nor `initializer` is present, the data type defaults to `Object`. |
71 |
| - |
72 |
| -- **Different Types.** You can specify different data types for different constants by using a separate `As` clause for each variable you declare. However, you cannot declare several constants to be of the same type by using a common `As` clause. |
73 |
| - |
74 |
| -- **Initialization.** You must initialize the value of every constant in `constantlist`. You use `initializer` to supply an expression to be assigned to the constant. The expression can be any combination of literals, other constants that are already defined, and enumeration members that are already defined. You can use arithmetic and logical operators to combine such elements. |
75 |
| - |
76 |
| - You cannot use variables or functions in `initializer`. However, you can use conversion keywords such as `CByte` and `CShort`. You can also use `AscW` if you call it with a constant `String` or `Char` argument, since that can be evaluated at compile time. |
77 |
| - |
78 |
| -## Behavior |
79 |
| - |
80 |
| -- **Scope.** Local constants are accessible only from within their procedure or block. Member constants are accessible from anywhere within their class, structure, or module. |
81 |
| - |
82 |
| -- **Qualification.** Code outside a class, structure, or module must qualify a member constant's name with the name of that class, structure, or module. Code outside a procedure or block cannot refer to any local constants within that procedure or block. |
83 |
| - |
84 |
| -## Example |
85 |
| - The following example uses the `Const` statement to declare constants for use in place of literal values. |
86 |
| - |
87 |
| - [!code-vb[VbVbalrStatements#13](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#13)] |
88 |
| - |
89 |
| -## Example |
90 |
| - If you define a constant with data type `Object`, the Visual Basic compiler gives it the type of `initializer`, instead of `Object`. In the following example, the constant `naturalLogBase` has the run-time type `Decimal`. |
91 |
| - |
92 |
| - [!code-vb[VbVbalrStatements#87](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#87)] |
93 |
| - |
94 |
| - The preceding example uses the <xref:System.Type.ToString%2A> method on the <xref:System.Type> object returned by the [GetType Operator](../../../visual-basic/language-reference/operators/gettype-operator.md), because <xref:System.Type> cannot be converted to `String` using `CStr`. |
95 |
| - |
| 11 | + |
| 12 | +Declares and defines one or more constants. |
| 13 | + |
| 14 | +## Syntax |
| 15 | + |
| 16 | +```vb |
| 17 | +[ <attributelist> ] [ accessmodifier ] [ Shadows ] |
| 18 | +Const constantlist |
| 19 | +``` |
| 20 | + |
| 21 | +## Parts |
| 22 | + |
| 23 | +`attributelist` |
| 24 | +Optional. List of attributes that apply to all the constants declared in this statement. See [Attribute List](../../../visual-basic/language-reference/statements/attribute-list.md) in angle brackets ("`<`" and "`>`"). |
| 25 | + |
| 26 | +`accessmodifier` |
| 27 | +Optional. Use this to specify what code can access these constants. Can be [Public](../../../visual-basic/language-reference/modifiers/public.md), [Protected](../../../visual-basic/language-reference/modifiers/protected.md), [Friend](../../../visual-basic/language-reference/modifiers/friend.md), [Protected Friend](../modifiers/protected-friend.md), [Private](../../../visual-basic/language-reference/modifiers/private.md), or [Private Protected](../../language-reference/modifiers/private-protected.md). |
| 28 | + |
| 29 | +`Shadows` |
| 30 | +Optional. Use this to redeclare and hide a programming element in a base class. See [Shadows](../../../visual-basic/language-reference/modifiers/shadows.md). |
| 31 | + |
| 32 | +`constantlist` |
| 33 | +Required. List of constants being declared in this statement. |
| 34 | + |
| 35 | +`constant` `[ ,` `constant` `... ]` |
| 36 | + |
| 37 | +Each `constant` has the following syntax and parts: |
| 38 | + |
| 39 | +`constantname` `[ As` `datatype` `] =` `initializer` |
| 40 | + |
| 41 | +|Part|Description| |
| 42 | +|----------|-----------------| |
| 43 | +|`constantname`|Required. Name of the constant. See [Declared Element Names](../../../visual-basic/programming-guide/language-features/declared-elements/declared-element-names.md).| |
| 44 | +|`datatype`|Required if `Option Strict` is `On`. Data type of the constant.| |
| 45 | +|`initializer`|Required. Expression that is evaluated at compile time and assigned to the constant.| |
| 46 | + |
| 47 | +## Remarks |
| 48 | + |
| 49 | +If you have a value that never changes in your application, you can define a named constant and use it in place of a literal value. A name is easier to remember than a value. You can define the constant just once and use it in many places in your code. If in a later version you need to redefine the value, the `Const` statement is the only place you need to make a change. |
| 50 | + |
| 51 | +You can use `Const` only at module or procedure level. This means the *declaration context* for a variable must be a class, structure, module, procedure, or block, and cannot be a source file, namespace, or interface. For more information, see [Declaration Contexts and Default Access Levels](../../../visual-basic/language-reference/statements/declaration-contexts-and-default-access-levels.md). |
| 52 | + |
| 53 | +Local constants (inside a procedure) default to public access, and you cannot use any access modifiers on them. Class and module member constants (outside any procedure) default to private access, and structure member constants default to public access. You can adjust their access levels with the access modifiers. |
| 54 | + |
| 55 | +## Rules |
| 56 | + |
| 57 | +- **Declaration Context.** A constant declared at module level, outside any procedure, is a *member constant*; it is a member of the class, structure, or module that declares it. |
| 58 | + |
| 59 | + A constant declared at procedure level is a *local constant*; it is local to the procedure or block that declares it. |
| 60 | + |
| 61 | +- **Attributes.** You can apply attributes only to member constants, not to local constants. An attribute contributes information to the assembly's metadata, which is not meaningful for temporary storage such as local constants. |
| 62 | + |
| 63 | +- **Modifiers.** By default, all constants are `Shared`, `Static`, and `ReadOnly`. You cannot use any of these keywords when declaring a constant. |
| 64 | + |
| 65 | + At procedure level, you cannot use `Shadows` or any access modifiers to declare local constants. |
| 66 | + |
| 67 | +- **Multiple Constants.** You can declare several constants in the same declaration statement, specifying the `constantname` part for each one. Multiple constants are separated by commas. |
| 68 | + |
| 69 | +## Data Type Rules |
| 70 | + |
| 71 | +- **Data Types.** The `Const` statement can declare the data type of a variable. You can specify any data type or the name of an enumeration. |
| 72 | + |
| 73 | +- **Default Type.** If you do not specify `datatype`, the constant takes the data type of `initializer`. If you specify both `datatype` and `initializer`, the data type of `initializer` must be convertible to `datatype`. If neither `datatype` nor `initializer` is present, the data type defaults to `Object`. |
| 74 | + |
| 75 | +- **Different Types.** You can specify different data types for different constants by using a separate `As` clause for each variable you declare. However, you cannot declare several constants to be of the same type by using a common `As` clause. |
| 76 | + |
| 77 | +- **Initialization.** You must initialize the value of every constant in `constantlist`. You use `initializer` to supply an expression to be assigned to the constant. The expression can be any combination of literals, other constants that are already defined, and enumeration members that are already defined. You can use arithmetic and logical operators to combine such elements. |
| 78 | + |
| 79 | + You cannot use variables or functions in `initializer`. However, you can use conversion keywords such as `CByte` and `CShort`. You can also use `AscW` if you call it with a constant `String` or `Char` argument, since that can be evaluated at compile time. |
| 80 | + |
| 81 | +## Behavior |
| 82 | + |
| 83 | +- **Scope.** Local constants are accessible only from within their procedure or block. Member constants are accessible from anywhere within their class, structure, or module. |
| 84 | + |
| 85 | +- **Qualification.** Code outside a class, structure, or module must qualify a member constant's name with the name of that class, structure, or module. Code outside a procedure or block cannot refer to any local constants within that procedure or block. |
| 86 | + |
| 87 | +## Example |
| 88 | + |
| 89 | +The following example uses the `Const` statement to declare constants for use in place of literal values. |
| 90 | + |
| 91 | +[!code-vb[VbVbalrStatements#13](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#13)] |
| 92 | + |
| 93 | +## Example |
| 94 | + |
| 95 | +If you define a constant with data type `Object`, the Visual Basic compiler gives it the type of `initializer`, instead of `Object`. In the following example, the constant `naturalLogBase` has the run-time type `Decimal`. |
| 96 | + |
| 97 | +[!code-vb[VbVbalrStatements#87](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbalrStatements/VB/Class1.vb#87)] |
| 98 | + |
| 99 | +The preceding example uses the <xref:System.Type.ToString%2A> method on the <xref:System.Type> object returned by the [GetType Operator](../../../visual-basic/language-reference/operators/gettype-operator.md), because <xref:System.Type> cannot be converted to `String` using `CStr`. |
| 100 | + |
96 | 101 | ## See also
|
97 | 102 |
|
98 | 103 | - <xref:Microsoft.VisualBasic.Strings.Asc%2A>
|
|
0 commit comments