Type Statement

Description

Used at module level to define a user-defined data type containing one or more elements.

Syntax

[Private | Public] Type varname
elementname [([subscripts])] As type
[ elementname [([subscripts])] As type]
. . .
End Type

The Type statement syntax has these parts:

Part

Description

Public

Used to declare user-defined types that are available to all procedures in all modules in all projects.

Private

Used to declare user-defined types that are available only within the module where the declaration is made.

varname

Name of the user-defined type; follows standard variable naming conventions.

elementname

Name of an element of the user-defined type. Element names also follow standard variable naming conventions, except that reserved words can be used.

subscripts

Dimensions of an array element. Use only parentheses when declaring an array whose size can change.

type

Data type of the element; may be Boolean, Integer, Long, Currency, Single, Double, Date, String, String * length (for fixed-length strings), Object, Variant, another user-defined type, or an object type.


Remarks

The Type statement can be used only at module level. Once you have declared a user-defined type using the Type statement, you can declare a variable of that type anywhere within the scope of the declaration. Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type.

Line numbers and line labels aren't allowed in Type...End Type blocks.

User-defined types are often used with data records, which frequently consist of a number of related elements of different data types.

The following example shows the use of fixed-size arrays in a user-defined type:


Type StateData
    CityCode (1 To 100) As Integer ' Declare a static array.
    County As String * 30
End Type
Dim Washington(1 To 100) As StateData

In the preceding example, StateData includes the CityCode static array, and the record Washington has the same structure as StateData.

When you declare a fixed-size array within a user-defined type, its dimensions must be declared with numeric literals or constants rather than variables.

The setting of the Option Base statement determines the lower bound for arrays within user-defined types.

See Also

Dim Statement, Private Statement, Public Statement, ReDim Statement, Static Statement.