Declaring User-Defined Types (UDTs)

 

User-defined Types (UDTs) are VB's way of implementing data structures. In C/C++, they are called structs, in Pascal and COBOL they are called records (they are also called "group" items in COBOL).

 

The following rules apply to UDTs:

 

The syntax for defining a UDT is:

 

      [Public | Private] Type TypeName

          Variable1  As datatype

          ...

          Variablen  As datatype

      End Type

 

For example, to define a UDT for an employee record, you might code the following:

 

      Public Type EmployeeRecord

          strEmpName    As String

          dtmHireDate   As Date

          sngHourlyRate As Single

      End Type

 

However, the definition alone is not enough. The Type definition is basically a "template" on which other variables are defined; the template itself does not store any data. To use a UDT, you must define a variable "As" the name following the keyword "Type" (in this case, "EmployeeRecord"). For example:

 

                Dim udtEmpRec     As EmployeeRecord

 

The above defines a variable called "udtEmpRec" which has the attributes defined by the structure "EmployeeRecord". Thus, it is "udtEmpRec" which you refer to in your procedural statements, NOT "EmployeeRecord". To reference an individual element of the structure, you must qualify that element with the "udt" variable that you defined. For example, the following code places data in the individual elements of  udtEmpRec:

 

      udtEmpRec.strEmpName = "JOE SMITH"

      udtEmpRec.dtmHireDate = #1/15/2001#

      udtEmpRec.sngHrlyRate = 25.50

 

You can declare any number of variables "As" the UDT that you have defined. For example:

 

      Dim udtEmpRec2          As EmployeeRecord

      Dim audtEmpRec(1 To 10) As EmployeeRecord

 

Note that the second definition above declares an array of the EmployeeRecord type. To refer to an individual field (such as strEmpName) in a particular "record number", you would code something like the following:

 

      audtEmpRec(5).strEmpName = "BILL JONES"

 

You can also have an array at the elementary level. For example:

 

      Public Type EmployeeRecord

          strEmpName                      As String

          dtmHireDate                     As Date

          sngHourlyRate                   As Single

          dblQuarterlyEarnings(1 To 4)    As Double

      End Type

 

 

If you have this declaration:

 

      Dim udtEmpRec As EmployeeRecord

 

Then a valid reference would be:

 

      Print udtEmpRec.dblQuarterlyEarnings(2)

 

 

 

If you have this declaration:

 

      Dim udtEmpRec(1 To 5) As EmployeeRecord

 

Then a valid reference would be:

 

      Print udtEmpRec(3).dblQuarterlyEarnings(2)

 

 

You can have nested UDTs. Consider the following declarations:

 

      Public Type EmployeeName

          FirstName     As String

          MidInit       As String

          LastName      As String

      End Type

 

      Public Type EmployeeRecord

          udtEmpName                      As EmployeeName

          dtmHireDate                     As Date

          sngHourlyRate                   As Single

          dblQuarterlyEarnings(1 To 4)    As Double

      End Type

 

      Dim udtEmpRec As EmployeeRecord

 

You would reference the EmployeeName fields as follows:

 

      udtEmpRec.udtEmpName.FirstName

      udtEmpRec.udtEmpName.MidInit

udtEmpRec.udtEmpName.LastName