Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Const Statement (Visual Basic)

  • 2021-09-15
Feedback

In this article

Declares and defines one or more constants.

Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ]Const constantlist

Parts

attributelist
Optional. List of attributes that apply to all the constants declared in this statement. SeeAttribute List in angle brackets ("<" and ">").

accessmodifier
Optional. Use this to specify what code can access these constants. Can bePublic,Protected,Friend,Protected Friend,Private, orPrivate Protected.

Shadows
Optional. Use this to redeclare and hide a programming element in a base class. SeeShadows.

constantlist
Required. List of constants being declared in this statement.

constant[ ,constant... ]

Eachconstant has the following syntax and parts:

constantname[ Asdatatype] =initializer

PartDescription
constantnameRequired. Name of the constant. SeeDeclared Element Names.
datatypeRequired ifOption Strict isOn. Data type of the constant.
initializerRequired. Expression that is evaluated at compile time and assigned to the constant.

Remarks

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, theConst statement is the only place you need to make a change.

You can useConst only at module or procedure level. This means thedeclaration 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, seeDeclaration Contexts and Default Access Levels.

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.

Rules

  • Declaration Context. A constant declared at module level, outside any procedure, is amember constant; it is a member of the class, structure, or module that declares it.

    A constant declared at procedure level is alocal constant; it is local to the procedure or block that declares it.

  • 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.

  • Modifiers. By default, all constants areShared,Static, andReadOnly. You cannot use any of these keywords when declaring a constant.

    At procedure level, you cannot useShadows or any access modifiers to declare local constants.

  • Multiple Constants. You can declare several constants in the same declaration statement, specifying theconstantname part for each one. Multiple constants are separated by commas.

Data Type Rules

  • Data Types. TheConst statement can declare the data type of a variable. You can specify any data type or the name of an enumeration.

  • Default Type. If you do not specifydatatype, the constant takes the data type ofinitializer. If you specify bothdatatype andinitializer, the data type ofinitializer must be convertible todatatype. If neitherdatatype norinitializer is present, the data type defaults toObject.

  • Different Types. You can specify different data types for different constants by using a separateAs clause for each variable you declare. However, you cannot declare several constants to be of the same type by using a commonAs clause.

  • Initialization. You must initialize the value of every constant inconstantlist. You useinitializer 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.

    You cannot use variables or functions ininitializer. However, you can use conversion keywords such asCByte andCShort. You can also useAscW if you call it with a constantString orChar argument, since that can be evaluated at compile time.

Behavior

  • Scope. Local constants are accessible only from within their procedure or block. Member constants are accessible from anywhere within their class, structure, or module.

  • 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.

Example 1

The following example uses theConst statement to declare constants for use in place of literal values.

' The following statements declare constants.Const maximum As Long = 459Public Const helpString As String = "HELP"Private Const startValue As Integer = 5

Example 2

If you define a constant with data typeObject, the Visual Basic compiler gives it the type ofinitializer, instead ofObject. In the following example, the constantnaturalLogBase has the run-time typeDecimal.

Const naturalLogBase As Object = CDec(2.7182818284)MsgBox("Run-time type of constant naturalLogBase is " &    naturalLogBase.GetType.ToString())

The preceding example uses theToString method on theType object returned by theGetType Operator, becauseType cannot be converted toString usingCStr.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo