This documentation is archived and is not being maintained.

11.2 Constant Expressions

Visual Studio .NET 2003

A constant expression is an expression whose value can be fully evaluated at compile time. The type of a constant expression can be Byte, Short, Integer, Long, Char, Single, Double, Decimal, Boolean, String, or any enumeration type. The following constructs are permitted in constant expressions:

  • Literals (including Nothing).
  • References to constant type members or constant locals.
  • References to members of enumeration types.
  • Parenthesized subexpressions.
  • Coercion expressions, provided the target type is one of the types listed above. Coercions to and from String are an exception to this rule and not allowed because String conversions are always done in the current culture of the execution environment at run time.
  • The +, and Not unary operators.
  • The +, , *, ^, Mod, /, \, <<, >>, &, And, Or, Xor, AndAlso, OrElse, =, <, >, <>, <=, and => binary operators, provided each operand is of a type listed above.
  • The following run-time functions:
    • Microsoft.VisualBasic.Strings.ChrW
    • Microsoft.VisualBasic.Strings.Chr, if the constant value is between 0 and 128
    • Microsoft.VisualBasic.Strings.AscW, if the constant string is not empty
    • Microsoft.VisualBasic.Strings.Asc, if the constant string is not empty

Constant expressions of an integral type (Long, Integer, Short, Byte) can be implicitly converted to a narrower integral type, and constant expressions of type Double can be implicitly converted to Single, provided the value of the constant expression is within the range of the destination type. These narrowing conversions are allowed regardless of whether permissive or strict semantics are being used.

ConstantExpression ::= Expression

See Also

4.8 Variables | 10.5 Event Statements | 11.1 Expression Classifications | Constants and Enumerations (Visual Basic Language Reference) | Constants and Enumerations (Visual Basic Language Concepts)

Show: