Description
Used at module level to set the default data type for variables and Function procedures whose names start with the specified characters.
Syntax
DefBool letterrange[,letterrange] . . .
DefInt letterrange[,letterrange] . . .
DefLng letterrange[,letterrange] . . .
DefCur letterrange[,letterrange] . . .
DefSng letterrange[,letterrange] . . .
DefDbl letterrange[,letterrange] . . .
DefDate letterrange[,letterrange] . . .
DefStr letterrange[,letterrange] . . .
DefObj letterrange[,letterrange] . . .
DefVar letterrange[,letterrange] . . .
The argument letterrange has the following syntax:
letter1[-letter2]
The arguments letter1 and letter2 specify the name range for which you can set a default data type. Each argument represents the first letter of the variable or Function procedure name and can be any letter of the alphabet. The case of letters in letterrange isn't significant.
Remarks
The statement name determines the data type:
Statement |
Data Type | |
DefBool |
Boolean | |
DefInt |
Integer | |
DefLng |
Long | |
DefCur |
Currency | |
DefSng |
Single | |
DefDbl |
Double | |
DefDate |
Date | |
DefStr |
String | |
DefObj |
Object | |
DefVar |
Variant |
For example, in the following program fragment, Message is a string variable:
DefStr A-Q . . . Message = "Out of stack space."
A Deftype statement affects only the module where it is used. For example, a DefInt statement in one module affects only the default data type of variables and Function procedures declared in that module; the default data type of variables in other modules is unaffected. If not explicitly declared with a Deftype statement, the default data type for all variables and all Function procedures is Variant.
When you specify a letter range, it usually defines the data type for variables that begin with letters in the lower 128 characters of the character set. However, when you specify the letter range A-Z, you set the default to the specified data type for all variables, including any that begin with international characters from the extended part of the character set (128-255).
Once the range A-Z has been specified, you can't further redefine any subranges of variables using Deftype statements. In fact, once a range has been specified, if you include a previously defined letter in another Deftype statement, an error occurs. However, you can explicitly specify the data type of any variable, defined or not, using a Dim statement with an As type clause. For example, you can use the following code at module level to define a variable as a Double even though the default data type is Integer:
DefInt A-Z Dim TaxRate As Double
Deftype statements don't affect elements of user-defined types since they must be explicitly declared.
See Also
Let Statement.
Example
This example shows various uses of the Deftype statements to set default data types of variables and function procedures whose names start with specified characters. The default data type can be overridden only by explicit assignment using the Dim statement. Deftype statements can only be used at the module-level.
' Variable names beginning with A through K default to Integer. DefInt A-K ' Variable names beginning with L through Z default to String. DefStr L-Z CalcVar = 4 ' Initialize Integer. StringVar = "Hello there" ' Initialize String. AnyVar = "Hello" ' Causes "Type mismatch" error. Dim Calc As Double ' Explicitly set the type to Double. Calc = 2.3455 ' Assign a Double. ' Deftype statements also apply to function procedures. CalcNum = ATestFunction(4) ' Call user-defined function. ' ATestFunction function procedure definition. Function ATestFunction(INumber) ATestFunction = INumber * 2 ' Return value is an integer. End Function