user defined data types

8
User Defined Data Types The Type Statement

Upload: wes

Post on 07-Jan-2016

31 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: User Defined Data Types

User Defined Data Types

The Type Statement

Page 2: User Defined Data Types

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.

Page 3: User Defined Data Types

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.

Page 4: User Defined Data Types

Type Statement

• Syntax:

[Private | Public] Type varname

elementname As type

[elementname As type]

. . .

End Type

Page 5: User Defined Data Types

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

Page 6: User Defined Data Types

' Declarations (in a standard module). Public Type SystemInfo

CPU As String VideoColors As IntegerCost As Currency PurchaseDate As Variant

End Type

Public Sub CreateSystem()Dim MySystem As SystemInfoMySystem.CPU = “Pentium 4”MySystem.VidoColors = 256MySystem.Cost = 1800.00MySystem.PurchaseDate = #1/1/02#

End Sub

Page 7: User Defined Data Types

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

Page 8: User Defined Data Types

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