User-Defined Data Types School of Business Eastern Illinois University User-Defined Data Types (Week 12, Friday 4/11/2003) © Abdou Illia, Spring 2003
Outline and Objectives Fixed-Length String Variables Records
Fixed-Length Strings Versus Variable-Length Strings So far, we have been using Variable-Length string variables Variable-Length string variables Characteristics Example Can contain strings of variable length Name = “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
Fixed-Length Strings Versus Variable-Length Strings String variables can be of Fixed-Length Fixed-Length string 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 If strings longer than n are assigned to a Fixed-Length string Only the n first characters will appear If strings shorter than n are assigned to a Fixed-Length string Spaces will be added to the end of the string
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
Records Students.txt “ST001”,”Andrea”,1982 “ST002”,”John”,1979 “ST003”,”Bill”,1981 Records Each data file is a collection of records Each record is a collection of fields In a record, each field has a name. Example: StudentID, State a type. Example: numeric, string a length which is a number of spaces allocated to it Records’ fields are usually hold Using fixed-length variables Records are user-defined data types Name:_ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _
Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ Using Records in VB In VB, each character of a string is stored in a piece of memory known as a byte So, a field of type String * n requires n bytes of memory 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: _ _ _ _ Using Records in VB 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 Public Type collegeData nom As String * 10 State As String * 2 YearFounded As Integer End Type Example
Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ Using Records in VB Declare the Record variable in the code window of the form (usually in an event procedure) using statements like: Dim RecordVariable As RecordType 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 using a reference like college.FieldName college college.nom college.state college.yearFounded
Name:_ _ _ _ _ _ _ _ _ _ State: _ _ Year Founded: _ _ _ _ Using Records in VB Assign values to each field using Assignment statements. Note: Value can be assigned - Using forms - Using data stored in a file - etc. Examples: college.nom = "Harvard" college.state = "MA" college.yearFounded = 1636 college.nom = txtCollegeName college.state = txtState college.yearFounded = Val(txtYear)
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
Examples ‘ In BAS Module Public Type address Name As String * 25 Street:_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ City: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ State: _ _ Zip: :_ _ _ _ _ _ _ _ _ _ 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
Exercises Write a Type block to declare the layout of a record with the given names and fields Name: Student; Fields: SSN, Name, Grade Name: Planet; Fields: PlanetName, DistanceFromSun Name: Sales; Fields: Store, AmountOfSales For each of the above three cases, write a procedure that declare a record variable and assign values to each of its fields.