Download presentation
Presentation is loading. Please wait.
1
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate events Create an object collection Use Object Browser Add properties to a form
2
VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined up until now Text box class Form or command button class VB supports writing object oriented (OO) programs (.NET)
3
Class Modules VB allows you to create your own new objects with a class module. User-created class modules have properties and methods. A tool such as the command button in a tool box is the object class.
4
Classes and Objects A command button on a form is an instance of the class—an object. Creating your own class is similar to adding a tool to the toolbox. A cookie cutter is a metaphor for a class. You create many cookies (objects) with the cutter (a class). Each cookie is a new instance of an object of the cookie cutter class.
5
Defining a New Class Defining your new class is like creating a new tool for the toolbox – the process does not create the object, only a definition of what the object looks like and how it will behave. You may then create as many instances of the class as you like e.g. your class may be employee, student, product etc
6
OOP Characteristics Object Oriented Programming (OOP) says that a true object oriented language has the following three characteristics; 1) encapsulation, 2) polymorphism, 3) inheritance.
7
OOP Characteristics Encapsulation refers to the characteristics—properties and behaviors (methods)—that are inside the object. You have one package that holds the definition of all properties, methods and events. It is also called data hiding because each keeps its data (properties) and procedures (methods) hidden. Polymorphism means different classes may have similarly named behaviors but that are implemented differently or react differently. For instance, Printer.print may behave differently from PictureBox.print, even though “print” is the method used for each. Inheritance is the ability to create new classes from existing ones, and the new ones take on all the behaviors and properties of the parents from which they are cloned.
8
VB & Object Oriented Programming Big advantage of OOP is the ability to reuse objects. Reusing objects saves time and effort You can create three objects from the same class, yet you can set their properties differently.
9
this represents the class Textbox these represent objects— instances of the class
10
Classes Design new behaviors and properties for classes you create You assign property values in your class You assign properties or reveal them with Property Get and Property Let procedures
11
Accessing the Values of a Class The Property Get procedure retrieves a property from a class, making it available to the outside world. The Property Let procedure assigns a value from the outside world to a property of the class. Property Let and Property Get are the only way to assign and retrieve property values for a class. Otherwise, there is no other “doorway” through which they can pass.
12
Form of the two procedures: [Public] Property Get Procedurename ([optional arg list]) [As datatype] statements in the procedure Procedurename = Propertyname End Property [Public] Property Let Procedurename ([optional arg list])IncomingValue _ [As datatype] statements in the procedure Propertyname = Procedurename End Property Example Public Property Get LastName() As String LastName = mstrLastName End Property Property Procedures
13
Creating a New Class 1) Define a new class module 2) Define the class properties 3) Add property procedures 4) Create a method 5) Save the class module
14
Define a new class module: Open a new project Select Add Class Module from the Project menu Click the New tab and choose Class Module; click Open Change the class name to one of your choosing (CProduct in this case) new name class module selection New tab
15
Defining Class Properties Declare properties of a class with variables (called instance variables) Variables are private and placed in General Declarations section Add property Let and Get procedures for each instance variable Code a method, which is a class’ behavior
16
Defining Class Properties Define three class properties (instance variables): Private mcurPriceAs Currency Private mintQuantityAs Integer Private mstrDescription As String Only procedures within the class can access the properties defined as private To provide a “window” through which properties can be retrieved or set, you code special methods called property procedures. Add Property Procedures, select Add Procedure from the Tools menu, name the property by naming the Procedure in the Name text box, and select options Property and Public. The Quantity property of the new class is defined this way—with two property procedures (also called accessor methods)
17
instance variables property procedures for the mintQuantity instance variable
18
Creating a New Object Using a Class Creating a class module defines a new class Code that creates and uses objects is placed in form and code modules, whereas a class must be placed in a class module Create an object of a class with the New keyword—called instantiating an object The New keyword creates a new instance of a class when the object is first used
19
Dimensioning objects with the New keyword: Dim EmployeeAs New cEmployee Private mInventoryAs New Cinventory Next, create a form that instantiates an object of the class CProduct
20
Create a collection by writing a new class module and then declaring an object variable (to create an object). Each object in the collection is known as an item. Each object has a position known as its index. A collection is declared with a Dim, Public, or Static statement just like any other object variable: Dim mProducts As New Collection VB always updates the Count property of a collection every time you add or remove an item from a collection.
21
The Add method adds an item to the collection. mProducts.Add Prod1 The Remove method removes an item according to its index in the collection. mProducts.Remove 2 It is best to provide a unique key to each object in a collection, rather than rely upon an index (see remove example above). It is difficult to associate an index with a particular product. Using a key value makes locating an object much easier. Add a string in the CProduct class: Private mstrProductCode As String Use the string to add an object to a collection or remove it from a collection
22
Using a Collection in a Form
23
Using the Object Browser Object Browser reveals names of objects, properties, methods, events, and VB constants View objects: View, Object Browser Object Browser is best way to look up an object’s available properties, methods, etc. Select an object and press F1 for help
24
ComboBox is selected object Method symbol Property symbol Event symbol Description
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.