Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic: An Object Oriented Approach 10 - Polymorphism.

Similar presentations


Presentation on theme: "Visual Basic: An Object Oriented Approach 10 - Polymorphism."— Presentation transcript:

1 Visual Basic: An Object Oriented Approach 10 - Polymorphism

2 Polymorphism The facility that permits objects of different classes to be interchangeable Different classes of object can have properties and methods that are compatible Same names Same parameters Same return types (for Functions or Property Gets) Considering only these methods, it is possible to send the same sequence of messages to objects of either class These classes are polymorphs

3 Polymorphism Example Sub ChangeObject(O As Object) O.Caption = “New Caption” O.BackColor = vbRed O.Width = 5000 O.Move 1000, 1000 End Sub This code can be applied to several different types of VB Controls CommandButtons Labels CheckBoxes Note parameter type – Object A generalism All types of VB object can be referred to by this type of object variable Controls User defined classes

4 More general – less secure We could also have sent a TextBox control to the example code shown This has no Caption property The code would cause an error that could only be detected at run-time Using Object as a general type should be considered bad practice Ideally, we should restrict the type to one that has all of the properties and methods used in the code Alternatively, we can check the object type before we access a property or method

5 Preventing Wrong-Type errors Sub ChangeObject(O As Object) If TypeOf(O) Is Label Or TypeOf (O) Is CommandButton Then O.Caption = “New Caption” ElseIf TypeOf(O) Is TextBox Then O.Text = “New Text” Else ‘ Does not have a caption or text property, best leave it alone End If ‘ ‘ ‘ ‘ End Sub This could get very tedious, and is prone to errors

6 Ideally we need Type-Safety Some way of stating that a class of objects is compatible with some general class Interfaces provide this security An interface is nothing more than a definition of Properties and Methods with no executable code involved Name of Property/Method Parameters (arguments) passed to them Values returned from them

7 Example of a Type Definition ‘ IData Interface class Public Sub StoreData(DataFile As Integer) End Sub Public Function LoadData(DataFile As _ Integer) End Function Public Property Get Data() As Variant End Property Public Property Set Data(NewValue As _ Variant) End Property Obvious feature – all procedures are empty Purely a statement of how a Property, Sub or Function would be called by other code A Message vocabulary This is placed in a class, but called an Interface

8 Type Declaration A class can declared so it Implements an Interface It can then be guaranteed to provide a set of Procedures/Methods There is no guarantee that these do anything sensible – just that they are compatible This is Interface Inheritance

9 e.g. An implementing class Implements IData ‘ This class must conform to IData Private … ‘ Declare member variables for class Public Sub IData_StoreData(DataFile As Integer) ‘ Does something to store information End Sub Public Function IData_ LoadData(DataFile As Integer) ‘ Does something to load information End Function Public Property Get IData_ Data() As Variant ‘ Returns an internal value End Property Public Property Set IData_ Data(NewValue As Variant) ‘ Updates an internal value End Property

10 Implementing class – key features VB Automatically provides templates of Properties and Methods to match interface No need to type (or mistype) Programmer is responsible for coding how a given class will implement a specific interface function Client program can use any class that implements the interface interchangeably

11 Interfaces Application Interchangeable Classes Interface Interface definitions are created to match an application Classes that implement the interface will ‘fit’ the application

12 Typical example of polymorphism Computer Aided Drawing (see chapter 10 Exercise) The program knows how to draw, move and edit an IShape object There are no IShape objects Several ‘concrete’ shape classes implement the IShape interface the drawing program uses The program can therefore draw, move etc. instances of these classes, even though there is no code in it that refers to them

13 CAD Example Private Sub DrawShapes() Dim I As IShape P.Cls For Each I In DrawList I.Draw P Next End Sub This is the abstract class, or superclass This is a collection of shape objects of various classes This statement draws a shape on PictureBox P, no matter what class it belongs to

14 CAD Program - result All of these are IShape objects, but are different classes


Download ppt "Visual Basic: An Object Oriented Approach 10 - Polymorphism."

Similar presentations


Ads by Google