Deftype Statements

See AlsoM5ALBT                 Example2YJ5IW2>Low

Used in the Declarations section of forms and modules2JU32VW to set the default data type3GYXY7 for variables and Function proceduresK6LBMC.

Syntax

DefInt letterrange [,letterrange] . . .

DefLng letterrange [,letterrange] . . .

DefSng letterrange [,letterrange] . . .

DefDbl letterrange [,letterrange] . . .

DefCur letterrange [,letterrange] . . .

DefStr letterrange [,letterrange] . . .

DefVar letterrange [,letterrange] . . .

Remarks

The argument letterrange has the following form:

letter1 [-letter2]

The arguments letter1 and letter2 specify the range of variables for which a default data type will be set.  Each argument represents the first letter of variable names and can be any uppercase or lowercase letter of the alphabet.  For instance, a letter range of A-D sets the default data type for all variables that begin with A, B, C, or D. The case of the letters in letterrange isn't significant.

The reserved word43US84 you use (type in Deftype) determines which of the following data types will be the default setting of variables in letterrange: Integer(DefInt), Long(DefLng), Single(DefSng), Double(DefDbl), Currency(DefCur), String(DefStr), or Variant8PHEAW3 (DefVar).  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 form or module in which it is used.  For example, a DefInt statement in one module affects only the default data type of variables declared in that module; the default data type of variables in forms and other modules is unaffected.  If not explicitly declared with a Deftype statement, the default data type for all variables and all Function procedures in forms and modules is Variant.

When a letter range is specified, it usually defines the data type for variables that begin with letters in the first 128 characters of the ANSI character set5221FB.  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 ANSI character set (128-255).

Once the range A-Z has been specified, you cannot 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 assign the data type of any variable, defined or not, using a Dim statement or a type-declaration character4TVC9NP.  For example, you can use the following code in the Declarations section of a form or module to define a variable as a Double even though the default data type is Integer:

DefInt A-Z
Dim TaxRate As Double

 

You can use the following statement in a procedure to explicitly assign the data type of Pi:

Pi! = 3.141593

 

A type-declaration character always takes precedence over a Deftype statement.  Deftype statements don't affect elements of user-defined types.


See Also

ANSI Character Set108ABF

Let StatementLANLET


Deftype Statement Example

The example uses the Deftype statements to set default data types of variables in two ranges.  Variables beginning with the letters A-K will be Integer; variables beginning with L-Z will be String.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

DefInt A-K

DefStr L-Z

Sub Form_Click ()

  NL = Chr(10)                     ' Define newline.

  IntVal = 2.3455

  Msg = "Notice that none of the string variables in this demo have "

  Msg = Msg & "the $ type-declaration character appended to their "

  Msg = Msg & "names." & NL & NL & "The DefStr statement defines "

  Msg = Msg & "the default for variables starting with letters 'L' "

  Msg = Msg & "through 'Z' to be of the string data type."

  MsgBox Msg

 

  Msg = "Notice that the variable 'IntVal' has been defined as an "

  Msg = Msg & "integer. Even though it is assigned a real value, it "

  Msg = Msg & "behaves as an integer, i.e., IntVal = "

  Msg = Msg & IntVal & " instead of 2.3455."

  MsgBox Msg                       ' Display message.

End Sub