User Defined Data Types The Type Statement. Creating Your Own Data Types You can combine variables...
-
Upload
elijah-wright -
Category
Documents
-
view
212 -
download
0
Transcript of User Defined Data Types The Type Statement. Creating Your Own Data Types You can combine variables...
![Page 1: 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.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/1.jpg)
User Defined Data Types
The Type Statement
![Page 2: 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.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/2.jpg)
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 The Type Statement. Creating Your Own Data Types You can combine variables of several different types to create user-defined types.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/3.jpg)
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 The Type Statement. Creating Your Own Data Types You can combine variables of several different types to create user-defined types.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/4.jpg)
Type Statement
• Syntax:
[Private | Public] Type varname
elementname As type
[elementname As type]
. . .
End Type
![Page 5: 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.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/5.jpg)
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 The Type Statement. Creating Your Own Data Types You can combine variables of several different types to create user-defined types.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/6.jpg)
' 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 The Type Statement. Creating Your Own Data Types You can combine variables of several different types to create user-defined types.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/7.jpg)
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 The Type Statement. Creating Your Own Data Types You can combine variables of several different types to create user-defined types.](https://reader036.fdocuments.us/reader036/viewer/2022083009/5697c00f1a28abf838cca50e/html5/thumbnails/8.jpg)
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