Declaring scalar variables implicitly

At module level or within a procedure, you can declare a variable implicitly by assigning a value to an identifier that you have not previously declared, as in the following example:

' Create an Integer variable without declaring it explicitly 
' and initialize it to 1.
counter% = 1

This has the same effect as the following explicit declaration and statement:

Dim counter%
counter% = 1

As with explicitly declared variables, the identifier has to be a legal one and not already in use as the name of a constant, variable, or procedure in the same scope in the same module. If you append a data type suffix to the variable name when you declare it, that suffix determines data type of the variable. If you don't append a data type suffix, one of two things happens: if the name begins with a character covered by an existing Deftype statement, the variable is implicitly declared to be of the data type appropriate to that statement. Otherwise, the variable is implicitly declared to be of type Variant. The same rules apply to explicitly declared variables if the declaration doesn't contain an As dataType clause and the variable name doesn't end in a data type suffix character:

' Declare a variable of type Variant.
Dim myVarV

Implicit declaration is a handy shortcut when you're writing a simple script, saving you the line of code that it would take to declare the variable explicitly. However, the line of code you save by collapsing the declaration of a variable and the assignment of a value into a single statement can be costly in an application of even moderate complexity for two reasons:

  • When you implicitly declare a variable of one of the scalar types by including the appropriate data type suffix, LotusScript® requires you to use that character whenever you subsequently refer to that variable. Omitting the data type suffix in referring to such a variable produces an error. The opposite is true of implicitly declared variables covered by Deftype statements: they are declared without a data type suffix, and you can't include one when you refer to them later in the application without producing an error.
  • If you omit the data type suffix in an implicit declaration and the identifier isn't covered by an existing Deftype statement, you implicitly declare a variable of type Variant, which is not necessarily what you want to do. While useful in many ways, Variants take up more storage space in memory than the other scalar types. And if you include a data type suffix when referring to a variable of type Variant, you receive an error.

For example:

' Create the Integer variable anInt without explicitly 
' declaring it and initialize it to 10.
anInt% = 10
Print anInt
' Produce "Name previously declared" error
' because LotusScript reads anInt (without suffix character)
' as an implicitly declared Variant variable, not
' the Integer variable anInt% (with suffix character).

' Create the Variant variable myVariantV without explicitly
' declaring it and initialize it to 10.
myVariantV = 10   
Print myVariantV%
' Produce "Type suffix mismatch" error
' because myVariantV (without suffix character) was declared
' as type Variant, but the suffix character % is only 
' appropriate for variables declared as type Integer.

If you want to disallow implicit declaration in a LotusScript® application, include the Option Declare statement at module level in a module that you plan to have loaded when the application runs. This statement tells LotusScript® to require explicit declarations for all your variables.

Note: The Boolean and Byte data types do not have a data type suffix character and therefore cannot be declared implicitly.

Deftype statements

You use a LotusScript® Deftype statement at module level to assign a default data type to variables whose names begin with a particular letter of the alphabet, don't end with a data type suffix character, and don't appear in an explicit declaration containing an As dataType clause. The statement must appear before any variables are declared in the module. The syntax is

Def type range [, range]...

where type is a suffix such as Cur or Dbl, which is an abbreviation of the name of a data type, and range is one or more consecutive letters of the alphabet.

For example:

' Implicitly declared variables beginning with
' A, a, B, b, C, or c will be of type Integer.
DefInt A-C
' Create the Integer variable anInt on the fly 
' and initialize it to 10.
anInt = 10
' Create a variable of type Variant on the fly
' and initialize it to 2. It's a Variant because
' it doesn't have a data type suffix character and doesn't
' begin with any of the characters in the specified
' DefInt range. 
smallIntV = 2