Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data.

Similar presentations


Presentation on theme: "Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data."— Presentation transcript:

1 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 1 Chapter 12: Programmer-Defined Types, Direct Access Files, and Object Classes

2 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 2 Programmer-Defined Data Types By using a programmer-defined data type, we can create an array that will store multiple data types. The Type statement is used to create a programmer-defined data type that is composed of standard data types or previously defined data types. Programmer-defined data types are usually defined in a Code module, so they are known to all modules of the project.

3 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 3 Creating a Programmer-Defined Data Type Public Type typename elementname1 as type elementname2 as type etc. End Type For Example: Public Type CollegeStudent lngIDNumber as Long strLastName as String*20 strFirstName as String*10 strMidInit as String*2 sngGPA as Single End Type

4 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 4 Declaring Variables as Programmer- Defined Data Types Before variables can be used, they must be declared to be of the programmer- defined data type; the type itself cannot be used as a variable. For example: Dim udtOneStudent as CollegeStudent

5 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 5 Structure of udtOneStudent Variable udtOneStudent lngIDNum strLastName strFirstName strMidInit sngGPA

6 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 6 Using the With Statement The With statement enables the programmer to work with multiple elements of a single object. With object Statements referring to object (using dot notation) End With For example: With udtOneStudent.lngIDNumber = CLng(txtIDNumber.Text).strLastName = txtLastName.Text.strFirstName = txtFirstName.Text.strMidInit = txtMiddleInitial.Text.sngGPA = CSng(txtGPA.Text) End With

7 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 7 Arrays and Programmer-Defined Data Types It is possible to create an array composed of programmer-defined data types. Arrays can be defined as an element of a programmer-defined data type, for example, Dim udtManyStudents(100) as CollegeStudent A programmer-defined data type can be included in other programmer-defined data types.

8 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 8 Dynamic Arrays Dynamic arrays are arrays whose size can grow or shrink as needed. They are declared without a maximum size and conserve memory, because space is set aside for them only when actually needed. When a dynamic array is needed, its size is declared using the ReDim statement to the actual maximum subscript that will be used within the procedure or function. Dynamic arrays become important when we are working with lists of programmer-defined variables where each variable can represent a great deal of data. The ReDim Preserve statement redimensions a dynamic array but preserves existing data.

9 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 9 Using the ItemData Property The ItemData property of a list box can be used to store information about the item in the list box, including a pointer to an array subscript. List.ItemData(index) = array subscript The ItemData property can be set using the NewIndex property of the list box. List.ItemData(list.NewIndex) = array subscript The array elements corresponding to items deleted from a list box are not physically deleted, but their array subscript does not appear as an ItemData property.

10 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 10 Using Direct Access Files A direct (or random) access file is a record-oriented file in which records can be accessed and manipulated in any order through their record number. Direct access files are opened with the Open statement, with the record length being defined by the Len() function. Open filename for Random as file number Len = Len(record) The Put statement is used to write records to a direct access file Put #file number, record number, variable name The Get statement is used to read records from the file. Get #file number, record number, variable name

11 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 11 Direct Access Files

12 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 12 Direct Access Files (cont.) We can find the number of records in a direct access file by dividing the number of bytes in the files found through the LOF() function by the length of one record. Number of Records = LOF(file number)\Len(record)

13 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 13 Creating Objects in VB Object-oriented programming is an efficient way to write computer programs that are easily combined and reused. Objects are self-contained modules that combine data and program code that cooperate in the program by passing strictly defined messages to one another. Key concepts in object-oriented programming are: –classes –encapsulation –inheritance

14 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 14 Object-oriented Programming (cont.) A class is a template with data and procedures from which objects are created. Classes are created first, and then objects are created from the classes. Class modules are saved as.cls files. Encapsulation implies that the class variables can never be addresses directly; they must be the objects properties and methods. Inheritance refers to the capability to create classes that are descendents of other classes-- something that is not possible in VB6.0.

15 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 15 Creating a Class Creating a Class module involves a six-step process to develop the procedures and functions that carry out the operations for an object derived from this class. These six steps are: 1.Add the Class module to the project. 2.Declare local variables to be used in the module. 3.Initialize the Class properties of the object. 4.Write statements to enable the class to have values assigned to its properties or to assign its properties to variables outside of the object. 5.Write the functions (methods) that will carry out processing within the class. 6.Save the module as a.cls file.

16 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 16 Creating Properties Variables in Classes are known as instance variables. Properties are created in Visual Basic via the Property Let and Property Get statements. Public Property Let propertyName(ByVal OutsideVar as type) mvarLocalVariable = OutsideVar End Property For example: Public Property Let FirstName(ByVal vData as string) mvarFirstName = vData End Property

17 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 17 Property Get Statements The Property Get Statement is used to determine an object property: Public Property Get propertyName() propertyName = mvarLocalVariable End Property Note that this is indeed a function, since a value is assigned to the property name within the function. For example, Public Property Get FirstName() FirstName = mvarFirstName End Property

18 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 18 Using the ClassBuilder Wizard The Class Builder wizard enables the programmer to build classes quickly and easily by adding properties and methods. The Property Builder is used to add properties to the class. It declares the necessary local variables and adds the Let and Get statements to create the properties. The Method Builder creates the first and last statements of the functions needed to create methods, including any arguments passed to the functions. The Class Builder creates the first and last statements for the Class_Initialize procedure.

19 Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data Types With Statement Dynamic Arrays Direct Access Files Creating Objects in VB Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy 19 Declaring and Using Objects Objects are declared at the form level or project level with the Dim...As New... statement. Once declared, an application object’s properties and methods can be used just like those of a visual object.


Download ppt "Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data."

Similar presentations


Ads by Google