Def... Statement

Syntax

DefBool letterrange[,letterrange]
DefByte letterrange [,letterrange]
DefInt letterrange [,letterrange]
DefLng letterrange [,letterrange]
DefCur letterrange [,letterrange]
DefSng letterrange [,letterrange]
DefDbl letterrange [,letterrange]
DefDec letterrange [,letterrange]
DefDate letterrange [,letterrange]
DefStr letterrange [,letterrange]
DefObj letterrange [,letterrange]
DefVar letterrange [,letterrange]


letterrange

Use: Required

Data Type: String

Use the syntax Letter1[-Letter2].

Unless you are using a strict code convention, this way of declaring variables is a way to become extremely confused with the data types used in your application in as short a time as possible.


Description

Used at module level to define a default data type for variables, arguments passed to procedures, and the return type for Function and Property Get procedures whose names start with the specified characters. For example, the statement DefStr s tells your program that every variable, function, and argument beginning with the letter "s" is a string data type.

Rules at a Glance

  • The statement name determines the data type:

    Statement Data Type
    DefBool Boolean
    DefByte Byte
    DefInt Integer
    DefLng Long
    DefCur Currency
    DefSng Single
    DefDbl Double
    DefDec Decimal
    DefDate Date
    DefStr String
    DefObj Object
    DefVar Variant

  • You can override the default data types defined with Def... by using the Dim statement.

  • Def... statements have scope only within the module in which they appear. There is no such thing as a public or global Def... statement for the project.

Example

DefStr s
DefDbl d
DefInt i

...

iMyInteger = 100
dMyDouble = 122345.899
sMyString = "Hello World"

Programming Tips and Gotchas

  • Elements of user-defined types aren't affected by Def... statements because the elements must be explicitly declared.

  • Def... statements must appear before all other declarations within the declarations section of a module.

  • If your code includes the Option Explicit statement, which indicates that your application relies on strong variable typing, Def... statements for the most part are rendered superfluous, since the Dim statement is still required to declare each variable. However, in this case, the Def... statement defines the data type of variables whose Dim statements don't specify a specific data type. For example, in the code fragment:

    Option Explicit
    DefStr s
    
    Public strMyVar1

    strMyVar1 has been defined as a string by the DefStr statement.

See Also

Dim Statement
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.201.168