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

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Classes, Exceptions, Collections, and Scrollable Controls
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Pointers.
An introduction to pointers in c
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
VBA Modules, Functions, Variables, and Constants
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Computers: Tools for an Information Age
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
ASP.NET Programming with C# and SQL Server First Edition
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 13: Object-Oriented Programming
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
C++ fundamentals.
Copyright © 2001 by Wiley. All rights reserved. Chapter 1: Introduction to Programming and Visual Basic Computer Operations What is Programming? OOED Programming.
Manipulating Strings.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Using Arrays and File Handling
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
Copyright © 2001 by Wiley. All rights reserved. Chapter 9: Introduction to Working with Databases in Visual Basic Database Concepts Relational Database.
JavaScript, Fourth Edition
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Working With Objects Tonga Institute of Higher Education.
Chapter 7 Database Basics Tables, Records, and Fields 7 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Module 4 Part 2 Introduction To Software Development : Programming & Languages Introduction To Software Development : Programming & Languages.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Learners Support Publications Object Oriented Programming.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
Introduction to OOP CPS235: Introduction.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Programming Right from the Start with Visual Basic .NET 1/e
Pointers and Dynamic Arrays
Microsoft Visual Basic 2005: Reloaded Second Edition
Programming the Web Using Visual Studio .NET
JavaScript: Functions.
Microsoft Visual Basic 2005 BASICS
CIS16 Application Development and Programming using Visual Basic.net
And now for something completely different . . .
Lecture Set 9 Arrays, Collections, and Repetition
Chapter 7 Database Basics
Presentation transcript:

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

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.

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

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

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

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

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.

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.

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.

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

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

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)

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

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.

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.

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

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

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.

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.