Const Statement

   
Const Statement

Syntax

 [   accessmodifier   ] Const   constantname   [As   type   ] =   constantvalue   
accessmodifier (optional; Keyword)

One of the keywords Public , Private , Protected , Friend , or Protected Friend . For more information, see Section 4.7 in Chapter 4.

constantname (required; String literal)

The name of the constant.

type (optional; Keyword)

The data type; it can be Byte , Boolean , Char , Short , Integer , Long , Single , Double , Decimal , Date , or String , as well as any of the data types defined in the Base Class Library.

constantvalue (required; Numeric or String)

A literal, constant, or any combination of literals and constants that includes arithmetic or logical operators, except Is .

Description

Associates a constant value with a name. This feature is provided to make code more readable. The name is referred to as a symbolic constant .

Rules at a Glance

  • The rules for constantname are the same for those of any variable: the name can be up to 255 characters in length and can contain any alphanumeric character, although it must start with an alphabetic character. In addition, the name can include almost any other character except a period or any of the data type definition characters ($, &, %, !).

  • The constantvalue expression cannot include any of the built-in functions or objects, although it can be a combination of absolute values and operators. The expression can also include previously defined constants. For example:

     Private Const CONST_ONE = 1 Private Const CONST_TWO = 2 Private Const CONST_THREE = CONST_ONE + CONST_TWO 
  • Scoping rules are the same as for variables . For more on scope, see Chapter 4.

  • If Option Strict is on, the data type of the constant must be defined by using the As type clause.

Example

 Private Const  MY_CONSTANT = 3.1417 

Programming Tips and Gotchas

  • Your code may be more readable if you take advantage of the fact that VB allows lengthy constant (and variable) names. This allows you to choose these names in a more meaningful way.

  • 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 are related , you should define them as members of an enumeration using the Enum statement.

See Also

Enum Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net