Type Statement |
No
[Private | Public] Type varname elementname [([subscripts])] As type [elementname [([subscripts])] As type] . . . End Type
Public
Use: Optional
Type: Keyword
Gives the user-defined type scope through all procedures in all modules in the project. Public and Private are mutually exclusive.
Private
Use: Optional
Type: Keyword
Restricts the scope of the user-defined type to those procedures within the same module. Public and Private are mutually exclusive.
varname
Use: Required
The name of the user-defined type.
elementname
Use: Required
Data Type: Any
The name of an element of the user-defined type.
subscripts
Use: Optional
Data Type: Numeric literal or constant
The dimensions of an array element.
type
Use: Required
The data type of the element.
Used at module level to define a user-defined type.
A user-defined type can contain one or more elements.
The Type statement can't be used within a procedure; it can be used only within the declaration section of a module.
In form and code modules, user-defined types are Public by default. You can reduce the scope and visibility of a user-defined type to the current module by using the Private keyword.
Until Version 6 of VB, publicly declared user-defined types were not permitted in class modules. Now, VB6 has introduced the remote user-defined types, which allow you to declare a property as a user-defined type, or have a class method return a user-defined type. The following snippet shows how to implement a remote user-defined type property within a class module:
Public Type udtTestType EmployeeNo As Integer EmployeeName As String End Type Private mudtTestType As udtTestType Public Property Get TestType() As udtTestType TestType = mudtTestType End Property Public Property Let TestType(udtVal As udtTestType) mudtTestType = udtVal End Property
A variable can be declared as being of a user-defined type by using the Dim, Private, Public, ReDim, or Static statements anywhere within the scope of the user-defined type.
The subscriptsclause uses the following syntax:
[lowerbound To] upperbound [, _ [lowerbound To] upperbound] . . .
When the optional lowerbound clause isn't present, the lower bound of the array defaults to that defined by the Option Base statement.
If an Option Base statement has not been used in the current module, the lower bound is zero.
lowerbound and upperbound must be stated as numeric literals or constants; variables can't be used.
Contrary to standard variable naming conventions, keywords can be used for the names of the elements of a user-defined type. However, this isn't recommended, since it makes code confusing to read.
type may be Byte, Boolean, Currency, Date, Double, Integer, Long, Object, Single, String, Variant, another user-defined type, or an object type.
The following table shows the values held by each data type when an element is initialized:
Data Type | Initial Value |
---|---|
Numeric | 0 |
Variable-length string | Zero-length string ("") |
Fixed-length string | Filled with zeros |
Variant | Empty |
Object | Nothing |
Date | Saturday 3o December 1899 12:00:00 |
Frequently, related items of information in an application are stored to parallel arrays. There is far less coding (and confusion) involved, however, if the elements are stored to a user-defined type instead. Using an array of user-defined types also tends to produce more readable code than using a multidimensional array.
User-defined types are central to building advanced data structures such as linked lists, stacks, and queues.
Visual Basic classes and collections of classes have largely taken over from user-defined types. A class module can be thought of as a direct development of the user-defined type. In a class module, properties replace the elements of a user-defined type. Furthermore, class modules contain subroutines and functions and can have scope outside of the current project. However, VB6 has somewhat revived the user-defined type by allowing user-defined types to be passed across process boundaries. For more information, see Chapter 4.
Using remote user-defined types requires NT 5, NT 4 Service Pack 4, or the latest version of DCOM95. Win98 also support remote user-defined types.
When using UDTs in an ActiveX Server (in VB6 onwards), a client of the server sees the UDT as an ActiveX object. Proof of this comes from the fact that you can use the TypeOf operator (which compares one object with another) with a user-defined type, as the following example demonstrates. This example also shows the use of the new vbUserDefinedType vartype constant, and the use of TypeName with a remote UDT. The first code snippet comes from an ActiveX server, which is referenced by a standard EXE project (the second code snippet) using the References dialog (this works only with early binding).
Code From Class Module in ActiveX DLL Project
Option Explicit Public Type udtTestType Name As String Age As Integer End Type
Code From Form Module in Standard EXE Project
Option Explicit Private udtTest As udtTestType Private Sub Command1_Click() If VarType(udtTest) = vbUserDefinedType Then Debug.Print "user defined type" End If 'returns "udtTestType" Debug.Print TypeName(udtTest) ' returns "Integer" Debug.Print TypeName(udtTest.Age) 'returns "String" Debug.Print TypeName(udtTest.Name) If TypeOf udtTest Is udtTestType Then Debug.Print "this is a udtTestType" End If End Sub