Const (statement)

Syntax

Const name [As type] = expression [,name [As type] = expression]...

Description

Declares a constant for use within the current script.

Comments

The name is only valid within the current Basic Control Engine script. Constant names must follow these rules:

1.     Must begin with a letter.

2.     May contain only letters, digits, and the underscore character.

3.     Must not exceed 80 characters in length.

4.     Cannot be a reserved word.

Constant names are not case-sensitive.

 

The expression must be assembled from literals or other constants. Calls to functions are not allowed except calls to the Chr$ function, as shown below:

Const s$ = "Hello, there" + Chr(44)

Constants can be given an explicit type by declaring the name with a type-declaration character, as shown below:

  Const a% = 5            'Constant Integer whose value is 5
  Const b# = 5            'Constant Double whose value is 5.0
  Const c$ = "5"          'Constant String whose value is "5"
  Const d! = 5            'Constant Single whose value is 5.0
  Const e& = 5            'Constant Long whose value is 5

 

The type can also be given by specifying the As type clause:

  Const a As Integer = 5     'Constant Integer whose value is 5
  Const b As Double = 5      'Constant Double whose value is 5.0
  Const c As String = "5"    'Constant String whose value is "5"
  Const d As Single = 5      'Constant Single whose value is 5.0
  Const e As Long = 5        'Constant Long whose value is 5

You cannot specify both a type-declaration character and the type:

  Const a% As Integer = 5    'THIS IS ILLEGAL.

If an explicit type is not given, then the Basic Control Engine script will choose the most imprecise type that completely represents the data, as shown below:

  Const a = 5              'Integer constant
  Const b = 5.5            'Single constant
  Const c = 5.5E200        'Double constant

 

Constants defined within a Sub or Function are local to that subroutine or function. Constants defined outside of all subroutines and function can be used anywhere within that script. The following example demonstrates the scoping of constants:

  Const DefFile ="default.txt"

  Sub Test1

    Const DefFile ="foobar.txt"
    MsgBox DefFile       'Displays "foobar.txt".
  End Sub

  Sub Test2

    MsgBox DefFile       'Displays "default.txt".
  End Sub

Example

This example displays the declared constants in a dialog box (crlf produces a new line in the dialog box).

Const crlf = Chr$(13) + Chr$(10)

Const greeting As String = "Hello, "
Const question1 As String = "How are you today?"

Sub Main()

  r = InputBox("Please enter your name","Enter Name")
  MsgBox greeting & r & crlf & crlf & question1
End Sub

See Also

DefType (statement); Let (statement); = (statement); Constants (topic).

More information

C