School of Business Eastern Illinois University UserDefined Data

School of Business Eastern Illinois University User-Defined Data Types (Week 12, Friday 4/11/2003) © Abdou Illia, Spring 2003

Outline and Objectives n Fixed-Length String Variables n Records 2

Fixed-Length Strings Versus Variable-Length Strings n 3 So far, we have been using Variable-Length string variables Characteristics Can contain strings of variable length Example 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 n String variables can be of Fixed-Length n Fixed-Length string variables – 4 Are declared using statements of the form: Dim Variable. Name As String * n Where n (representing the length of the string) is a positive integer – n If strings longer than n are assigned to a Fixed-Length string – n Are named following the same rules as other variable types 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 cmd. Go_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" pic. Output. Cls pic. Output. Print "123456789012345" pic. Output. Print city & "***" pic. Output. Print town & "***" pic. Output. Print municipality & "***" End Sub 5

Records Students. txt “ST 001”, ”Andrea”, 1982 6 “ST 002”, ”John”, 1979 “ST 003”, ”Bill”, 1981 n Each data file is a collection of records n Each record is a collection of fields n In a record, each field has – – – n Records’ fields are usually hold – n a name. Example: Student. ID, State a type. Example: numeric, string a length which is a number of spaces allocated to it Using fixed-length variables Records are user-defined data types Name: _ _ _ _ State: _ _ Year Founded: _ _

Using Records in VB Name: _ _ _ _ _ State: _ _ Year Founded: _ _ 7 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

Using Records in VB Name: _ _ _ _ _ State: _ _ Year Founded: _ _ 8 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: Example [Public/Private] Type Record. Type Public Type college. Data Field. Name 1 As Field. Type 1 nom As String * 10 State As String * 2 Field. Name 2 As Field. Type 2 Year. Founded As Integer ……. . End Type

Using Records in VB Name: _ _ _ _ _ State: _ _ Year Founded: _ _ 9 2) Declare the Record variable in the code window of the form (usually in an event procedure) using statements like: Dim Record. Variable As Record. Type n Example: Dim college As college. Data – – 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. Field. Name college. nom college. state college. year. Founded

Using Records in VB Name: _ _ _ _ _ State: _ _ Year Founded: _ _ 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: college. nom = "Harvard" college. state = "MA" college. year. Founded = 1636 college. nom = txt. College. Name college. state = txt. State college. year. Founded = Val(txt. Year) 10

Examples ‘ In General declaration section of the form Private Type vitamins A As Single B As Single End Type Private Sub cmd. Display_Click() Dim Minimum As vitamins Minimum. A = 500 Minimum. B = 200 pic. Output. Print Minimum. A pic. Output. Print Minimum. B End Sub 11

Examples Name: _ _ _ _ _ Street: _ _ _ _ _ _ _ City: _ _ _ _ _ State: _ _ Zip: : _ _ _ _ _ ‘ 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 cmd. Display_Click() Dim Institution As address Institution. Name = "White. House" Institution. Street = “ 1600 Pennsylvania Avenue“ Institution. City = “Whashington“ Institution. State = “DC“ Institution. Zip = “ 20500“ End Sub 12

Exercises 13 Write a Type block to declare the layout of a record with the given names and fields 1. Name: Student; Fields: SSN, Name, Grade 2. Name: Planet; Fields: Planet. Name, Distance. From. Sun 3. Name: Sales; Fields: Store, Amount. Of. Sales For each of the above three cases, write a procedure that declare a record variable and assign values to each of its fields.
- Slides: 13