Title: User Defined Data Types
1User Defined Data Types
2Creating Your Own Data Types
- You can combine variables of several different
types to create user-defined types. - User-defined types are useful when you want to
create a single variable that holds several
related pieces of information.
3Creating Your Own Data Types
- You create a user-defined type with the Type
statement, which must be placed in the
Declarations section of a module. - User-defined types can be declared as Private or
Public with the appropriate keyword.
4Type Statement
- Syntax
- Private Public Type varname
- elementname As type
- elementname As type
- . . .
- End Type
5Type Statement Example
- Type EmployeeRecord
- ID As Integer
- Name As String
- Address As String
- Phone As Long
- HireDate As Date
- End Type
- Public Sub CreateRecord()
- Dim MyRecord As EmployeeRecord
- MyRecord.ID 12003
- End Sub
6' Declarations (in a standard module). Public
Type SystemInfo CPU As String VideoColors As
Integer Cost As Currency PurchaseDate As
Variant End Type Public Sub CreateSystem() Dim
MySystem As SystemInfo MySystem.CPU Pentium
4 MySystem.VidoColors 256 MySystem.Cost
1800.00 MySystem.PurchaseDate 1/1/02 End
Sub
7Private Sub cmdProcess_Click() picBox.Cls Dim
college As collegeData Call GetDat(college)
Call DisplayStatement(college) End Sub Private
Sub DisplayStatement(school As collegeData)
picBox.Print school.name " was founded in " _
school.yearFounded picBox.Print " in "
school.state End Sub Private Sub GetDat(school
As collegeData) school.name txtCollege.Text
school.state txtState.Text school.yearFounded
Val(txtYear.Text) End Sub
Public Type collegeData name As String 30
state As String 2 yearFounded As Integer End
Type
8Dim arrDog(1 To 5) As Dog Dim Index As
Integer Private Sub cmdAddData_Click()
arrDog(Index).Breed txtBreed.Text
arrDog(Index).Color txtColor.Text
arrDog(Index).Age Val(txtAge.Text) Index
Index 1 'clear textboxes for new entry
txtBreed.Text "" txtColor.Text ""
txtAge.Text "" End Sub Private Sub
cmdPrintData_Click() For i 1 To 5
picOutput.Print arrDog(i).Breed " "
picOutput.Print arrDog(i).Color " "
picOutput.Print arrDog(i).Age Next i End
Sub Private Sub Form_Load() Index 1 End Sub
Public Type Dog Breed As String Color As
String Age As Integer End Type