1 / 8

User Defined Data Types

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.

wes
Download Presentation

User Defined Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. User Defined Data Types The Type Statement

  2. 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.

  3. 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.

  4. Type Statement • Syntax: [Private | Public] Type varname elementname As type [elementname As type] . . . End Type

  5. 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

  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

  7. 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

  8. 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

More Related