Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.

Similar presentations


Presentation on theme: "Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET."— Presentation transcript:

1 Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET

2 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 2 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object –Text, ForeColor, Checked, Visible, Enabled Methods - like a verb, an action or behavior, something the object can do or have done to it –ShowDialog, Focus, Clear, ToUpper, ToLower Events - object response to user action or other events –Click, Enter, Activate

3 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 3 Thus Far... Since Chapter 1 we have been using objects Up until now the classes for all objects used have been predefined We have created new objects for these classes by using the controls in the Toolbox VB allows programmers to create their own object types by creating a Class Module

4 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 4 Class and Instance When we add a button object from the button tool in the toolbox to the form we are creating an Instance of the Button Class The button object is an Instance of the Button Class Every button on the form is an Instance Defining your own Class is like creating a new tool for the Toolbox

5 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 5 "Cookie Analogy" Class = Cookie cutter Instantiate = Making a cookie using the cookie cutter Instance = Newly made cookie Properties of the Instance may have different values –Icing property can be True or False –Flavor property could be Lemon or Chocolate

6 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 "Cookie Analogy" (cont.) Methods = Eat, Bake, or Crumble Events = Cookie crumbling all by itself and informing you Methods and Events are often difficult to distinguish!

7 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 7 Encapsulation Combination of characteristics of an object along with its behavior in "one package" Cannot make object do anything it doesn't already "know" how to do Cannot make up new properties, methods, or events Sometimes referred to as data hiding;an object can expose only those data elements and procedures that it wishes

8 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 8 Inheritance Ability to create a new class from an existing class Purpose of Inheritance is reusability For example, each form created is inherited from the existing Form class –Original class is called Base Class, Superclass, or Parent Class –Inherited class is called Subclass, Derived Class, or Child Class

9 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 9 Inheritance (cont.) Examine 1st line of code for a form in the Editor Public Class Form1 Inherits System.Windows.Forms.Form Inherited Class, Derived Class Subclass, Child Class Base Class, Superclass, Parent Class

10 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 10 Inheritance Example Base Class –Person Subclasses –Employee –Customer –Student Person -Name -Address -Phone EmployeeStudentCustomer

11 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 11 Polymorphism Different classes of objects may have behaviors that are named the same but are implemented differently Programmers can request an action without knowing exactly what kind of object they have or exactly how it will carry out the action

12 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 12 Polymorphism Implemented Overloading –Argument type determines which version of a method is used –Example: MessageBox.Show method Overriding –Refers to a class that has the same method name as its base class –Method in subclass takes precedence

13 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 13 Reusability The main purpose behind OOOP and Inheritance in particular New classes created with Class Module can be used in multiple projects Each object created from the class can have its own properties

14 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 14 Multitier Applications Common use of classes is to create multitier applications Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines Goal is to create components that can be combined and replaced

15 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 15 Three-tier Model Most common implementation of multitier Presentation TierBusiness TierData Tier User Interface Forms Controls Menus Business Objects Validation Calculations Business Logic Business Rates Data Retrieval Data Storage

16 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 16 Instantiating An Object Creating a new object based on a class Create an instance of the class by using the New keyword and specify the class General Form New className ( )

17 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 17 Examples of Instantiating An Object Dim fntMyFont = New Font ("Arial", 12) lblMsg.Font = fntMyFont OR lblMsg.Font = New Font ("Arial", 12)

18 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 18 Specifying a Namespace In your projects, you have noticed the Inherits clause when VB creates a new form class Public Class Form1 Inherits System.Windows.Forms.Form Name of the Class Namespace

19 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 19 Namespace (cont.) Entire namespace is not needed for any classes in the namespaces that are automatically included in a Windows Forms project which include –System –System.Windows.Forms –System.Drawing When referring to classes in a different namespace –Write out the entire namespace –Add and Imports Statement to include the namespace

20 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 20 Class Design - Analyze: Characteristics of your new objects –Characteristics will be properties –Define the properties as variables in the class module Behaviors of your new objects –Behaviors will be methods –Define the methods as sub procedures and functions in the class module

21 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 21 Create a New Class Project, Add Class Module Add New Item dialog, choose Class Name the Class Define the Class properties Code the methods

22 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 22 Properties of a Class Define variables inside the Class Module by declaring them as Private Do not make Public-that would violate Encapsulation (each object should be in charge of its own data) Private mintPatientNum as Integer Private mdtmDate as Date Private mstrLastName as String

23 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 23 Assign Values to Properties Write special property procedures to –Pass the values to the class module –Return values from the class module Name used for property procedure is the name of the property seen by the outside world Property Get –Retrieves property values from a class –Like a function must return a value Property Set –Sets or assigns values to properties

24 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 24 Property Procedure General Form Private ClassVariable As DataType [Public] Property PropertyName As DataType Get PropertyName = ClassVariable End Get Set (ByVal Value As DataType) [statements, such as validation] ClassVariable = Value End Set End Property

25 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 25 Read-Only Properties In some instances a value for a property should only be retrieved by an object and not changed –Create a read-only property by using the ReadOnly modifier –Write only a Get portion of the property procedure [Public] ReadOnly Property PropertyName As DataType

26 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 26 Code a Method Create methods by adding sub procedures and functions for the behaviors to the class module The sub procedures and functions should be defined as Public

27 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 27 Create Regions Regions of code allow sections of code to be hidden in the same way that the Editor hides Windows generated code To add a region –Include #Region Statement followed by a string literal giving the region's name –#End Region tag will be added automatically –Write code between the 2 statements

28 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 28 Creating a New Object Using a Class Similar to creating a new tool for the toolbox but not yet instantiating Declare a variable for the new object with datatype of the class Then, instantiate the object using the New keyword

29 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 29 Best Practices You may declare and insttantiate an object at the same time but not best practice Should declare the variable separately in the Declarations section Instantiate the object –Only when(if) it is needed –Inside a Try/Catch block for error handling (Try/Catch block must be inside a procedure)

30 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 30 Instance versus Shared Variables Instance variables or properties –Separate memory location for each instance of the object Shared variables or properties –Single memory location that is available for ALL objects of a class –Can be accessed without instantiating an object of the class –Use the Shared keyword to create Shared Methods can also be created

31 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 31 Constructors and Destructors Constructor –Method that automatically executes when an object is instantiated –Create by writing a Public Sub New procedure Destructor –Method that automatically executes when an object is destroyed –Create by writing a Finalize procedure –Usage discouraged by Microsoft

32 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 32 Overloading Overloading means that 2 methods have the same name but a different list of arguments (the signature) Create by giving the same name to multiple procedures in your class module, each with a different argument list

33 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 33 Parameterized Constructor Constructor that requires arguments Allows arguments to be passed when creating an object Can be used to assign initial property values

34 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 34 Garbage Collection Feature of.NET Common Language Runtime (CLR) that cleans up unused components Periodically checks for unreferenced objects and releases all memory and system resources used by the objects Microsoft recommends depending on Garbage Collection rather than Finalize procedures

35 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 35 Inheritance Implemented New class can –Be based on another class (base class) –Inherit the properties and methods (but not constructors) of the base class, which can be One of the VB existing classes Your own class Designate Inheritance by adding the Inherits statement referencing the base class

36 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 36 Overriding Methods Methods created in subclass with the same name and the same argument list Subclass will use the method in its own class rather than that in the base class To override a method –Declare the base class method with the Overridable keyword –Declare the subclass method with the Overrides keyword

37 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 37 Creating a Base Class Strictly for Inheritance Classes can be created strictly for inheritance and are never instantiated Subclasses are created and instantiated which inherit the base class properties and methods For such a base class include the MustInherit modifier on the class declaration

38 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 38 Inheriting Form Classes Many projects require several forms Create a base form and inherit the visual interface to new forms Base form inherits from System.Windows.Forms.Form Subclass from inherits from Base form

39 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 39 Creating Inherited Form Class Project menu, Add Windows Form Modify the Inherits Statement to inherit from bass form using project name as the namespace OR Project menu, Add Inherited Form In dialog select name of Base form

40 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 40 Referencing Values on a Different Form Use the identifier for the other form's instance to refer to controls on different form General Syntax Example FormInstance.ControlName.Property Dim frmSumInstance as New frmSum( ) frmSumInstance.lblTotal.Text=FormatCurrency(mdecTotal)

41 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 41 Object Browser Use it to view the names, properties, methods, events and constants of VB objects, your own objects, and objects available from other applications Accessed –Tab in Editor Window –View menu, Other Window, Object Browser

42 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 42 Opening Object Browser from Toolbar

43 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 43 Object Browser Objects list Browse list Find Symbol Namespace icons Member list Class icon Constants icon Method icon Property icon Event icon

44 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 44 Examining VB Classes Members of System.Windows.Forms.MessageBox Class

45 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 45 Examining VB Classes (cont.) Display the MessageBoxButtons Constants


Download ppt "Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET."

Similar presentations


Ads by Google