Download presentation
Presentation is loading. Please wait.
1
User-Defined Data Types School of Business Eastern Illinois University © Abdou Illia, Fall 2002 (Week 13, Monday 11/18/2002)
2
2 Outline and Objectives n Fixed-Length String Variables n Records
3
3 Fixed-Length Strings Versus Variable-Length Strings n So far, we have been using Variable-Length string variables CharacteristicsExample Can contain strings of variable lengthName = “Lee” or Name = “Jimmy” Are declared without mentioning a specific length Dim Name As String Have a VB built-in data type (String) Could be used in programs without being declared Variable-Length string variables
4
4 Fixed-Length Strings Versus Variable-Length Strings n String variables can be of Fixed-Length n Fixed-Length variables – Are declared using statements of the form: Dim VariableName As String * n Where n (representing the length of the string) is a positive integer – Are named following the same rules as other variable types n If strings longer than n are assigned to a Fixed-Length string – Only the n first characters will appear n If strings shorter than n are assigned to a Fixed-Length string – Spaces will be added to the end of the string
5
5 Fixed-Length strings: Example Private Sub cmdGo_Click() ‘ Illustrate fixed-length strings Dim town As String Dim city As String * 9 Dim municipality As String * 12 town = "Chicago" city = "Chicago" municipality = "Chicago" picOutput.Cls picOutput.Print "123456789012345" picOutput.Print city & "***" picOutput.Print town & "***" picOutput.Print municipality & "***" End Sub
6
6 Records n Each data file (ex: a sequential file) is a collection of records n Each record is a collection of fields n Each field in the record has – a name. Ex: StudentID, State – a type. Ex: numeric, string – a length which is a number of spaces allocated to it n Records’ fields are usually hold – Using fixed-length variables n Records are user-defined data types “ST001”,”Andrea”,1982 “ST002”,”John”,1979 “ST003”,”Bill”,1981 Students.txt Name:_ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _
7
7 Using Records in VB n In VB, each character of a string is stored in a piece of memory known as a byte n So, a field of type String * n requires n bytes of memory n In VB, numbers (Integer, Single, etc.) are stored in a different manner than strings – Integer numbers are always stored using 2 bytes – Single numbers are always stored using 4 bytes Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _
8
8 Using Records in VB 1) Declare the record’s layout in General declaration section of a form or in a BAS Module (Project/Add Module…) using statements of the form: [Public/Private] Type RecordType FieldName1 As FieldType1 FieldName2 As FieldType2 …….. End Type Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ Public Type collegeData nom As String * 10 State As String * 2 YearFounded As Integer End Type Example
9
9 Using Records in VB 2) Declare the Record variable in the Form code (usually in an event procedure) using statements of the form: Dim RecordVariable As RecordType n Example: Dim college As collegeData – Make VB create necessary memory space to hold data in the three fields of the record variable “college”. – Each field can be accessed a reference like college.FieldName Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ college.nom college.state college.yearFounded college
10
10 Using Records in VB 3) Assign values to each field using Assignment statements. Note: Value can be assigned - Using forms - Using data stored in a file - etc. n Examples: Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ college.nom = " Harvard " college.state = " MA " college.yearFounded = 1636 college.nom = txtCollegeName college.state = txtState college.yearFounded = Val(txtYear)
11
11 Examples ‘ In General declaration section of the form Private Type vitamins A As Single B As Single End Type Private Sub cmdDisplay_Click() Dim Minimum As vitamins Minimum.A = 500 Minimum.B = 200 picOutput.Print Minimum.A picOutput.Print Minimum.B End Sub
12
12 Examples ‘ In BAS Module Public Type address Name As String * 25 Street As String * 30 City As String * 20 State As String * 2 Zip As String * 10 End Type Private Sub cmdDisplay_Click() Dim Institution As address Institution.Name = "WhiteHouse" Institution.Street = “1600 Pennsylvania Avenue“ Institution.City = “Whashington“ Institution.State = “DC“ Institution.Zip = “20500“ End Sub Name: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Street: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ City: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ State: _ _ Zip: : _ _ _ _ _ _ _ _ _ _
13
13 Exercises 1. Name: Student; Fields: SSN, Name, Grade 2. Name: Planet; Fields: PlanetName, DistanceFromSun 3. Name: Sales; Fields: Store, AmountOfSales Write a Type block to declare the layout of a record with the given names and fields For each of the above three cases, write a procedure that declare a record variable and assign values to each of its fields.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.