80 likes | 256 Views
User Defined Data Types. The Type Statement. Creating 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.
E N D
User Defined Data Types The Type Statement
Creating 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.
Creating 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.
Type Statement • Syntax: [Private | Public] Type varname elementname As type [elementname As type] . . . End Type
Type 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
' 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
Private 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
Dim 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