Home

User-Defined Types

 

Introduction

In a Visual Basic application, you can create a custom data type by using one of the available types or by combining some them to get a new one. To do this, you must create a new module for the new type. You start the new type with the Type keyword followed by the name of the new type. The create of the type ends with the End Type expression:

Type SampleType
      
End Type

Between the Type line and the End Type line, you can declare one or more existing types as variables. That is, each declaration can be made of a name for a variable, followed by As, and followed by a known data type. Here is an example:

Type Employee
    DateHired As Date
    FullName As String
    IsMarried As Boolean
    HourlySalary As Double
End Type

Using a Programmer-Defined Data Type

After creating the type, in the procedure or event where you want to use it, declare a variable based on it. To access any of the member variables of the type, enter the name of its variable, followed by a period operator, and followed by the name of the member variable. After accessing a member variable of a type, you can initialize, change its value, or assign it to another variable. Here is an example:

Private Sub cmdCreate_Click()
    Dim Contractor As Employee
    
    Contractor.DateHired = #12/4/2000#
    Contractor.FullName = "Leslie Abramson"
    Contractor.IsMarried = True
    Contractor.HourlySalary = 20.15
    
    txtDateHired = CStr(Contractor.DateHired)
    txtFullName = Contractor.FullName
    chkIsMarried.Value = Contractor.IsMarried
    txtHourlySalary = Contractor.HourlySalary
End Sub

Home Copyright © 2005-2011 FunctionX, Inc.