Chapter 6 OOP: Creating Object-Oriented Programs - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Chapter 6 OOP: Creating Object-Oriented Programs

Description:

Defining your own Class is like creating a new tool for the Toolbox. 6 'Cookie Analogy' ... Person -Name -Address -Phone. Employee. Student. Customer. Dept ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 51
Provided by: richard145
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 OOP: Creating Object-Oriented Programs


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

2
Object Terminology Review
  • Object - like a noun, a thing
  • Buttons, Text Boxes, Labels
  • Customer, Book, Library
  • Properties - like an adjective, characteristics
    of object
  • Text, ForeColor, Checked, Visible, Enabled,
    Price, Quantity

3
Object Terminology Review
  • Methods - like a verb, an action or behavior,
    something the object can do or have done to it
  • ShowDialog, Focus, Clear, ToUpper,
    ToLower,Extendprice, CheckOut
  • Events - object response to user action or other
    events
  • Click, Enter, Activate

4
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

5
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

6
"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

7
"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!
8
Encapsulation
  • Combination of characteristics/properties/attribut
    es of an object along with its
    behavior/methods/events 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 hidingan object
    can expose only those data elements and
    procedures that it wishes

9
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

10
Inheritance (cont.)
  • Examine 1st line of code for a form in the Editor

Inherited Class, Derived Class Subclass, Child
Class
Public Class Form1 Inherits System.Windows.Forms.
Form
Base Class, Superclass, Parent Class
11
Inheritance Example
  • Base Class
  • Person
  • Subclasses
  • Employee
  • Customer
  • Student

Dept
Balance
GPA
12
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

13
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

14
Reusability
  • The main purpose behind OOP 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

15
Multitier (n-tier) 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

16
Three-tier Model
  • Most common implementation of multitier

17
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 ( )
18
Examples of InstantiatingAn Object
Dim fntMyFont New Font ("Arial",
12) lblMsg.Font fntMyFont OR lblMsg.Font
New Font ("Arial", 12) NEW is a constructor
method
19
Specifying a Namespace
  • In your projects, you have noticed the Inherits
    clause when VB creates a new form class

Name of the Class
Public Class Form1 Inherits System.Windows.Forms.
Form
Namespace
20
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 an Imports Statement to include the namespace

21
lblMessage.Font NEW System.Drawing.Font(Arial,
12) Or IMPORTS System.Drawing lblMessage.Font
NEW Font(Arial,12)
22
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

23
Create a New Class
  • Project, Add Class Module
  • Add New Item dialog, choose Class
  • Name the Class
  • Define the Class properties
  • Code the methods

24
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

25
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

26
Property Procedure General Form
Private ClassVariable As DataType Public
Property PropertyName As DataType Get PropertyN
ame ClassVariable End Get Set (ByVal Value
As DataType) statements, such as
validation ClassVariable Value End Set End
Property
27
Property Procedure General Form
Private mintQtyOrdered As Integer Public
Property QuantityOrdered As Integer Get
QuantityOrdered mintQtyOrdered End Get Set
(ByVal Value As DataType) statements, such as
validation mintQtyOrdered Value End
Set End Property
28
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
29
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

30
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

31
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

32
Best Practices
  • You may declare and instantiate 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)

33
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
34
Shared Properties
  • ReadOnly
  • Otherwise violates encapsulation

35
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

36
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

37
Parameterized Constructor
  • Constructor that requires arguments
  • Allows arguments to be passed when creating an
    object
  • Can be used to assign initial property values
  • If parameterizing NEW()
  • Must have a NEW() with no parameters

38
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

39
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

40
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

41
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

42
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

43
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

44
Creating Inherited Form Class
  • Cannot Delete inherited controls
  • Make visible false
  • Overriding inherited events
  • Cant double click control
  • Copy and Paste
  • In Code window
  • Select Overrides
  • Select event

45
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.TextFormatCurrency(mBoo
kSale.mdecTotal)
46
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

47
Opening Object Browser from Toolbar
48
Object Browser
49
Examining VB Classes
Members of System.Windows.Forms.MessageBox Class
50
Examining VB Classes (cont.)
Display the MessageBoxButtons Constants
Write a Comment
User Comments (0)
About PowerShow.com