Chapter 5 POO in
Chapter 5 POO in
Programming in Visual
Basic .NET
Overview
Defining Classes
Creating and Destroying Objects
Inheritance
Interfaces
Working with Classes
Introduction to Object-Oriented Concepts
What Is a Class?
Object
Object
123
245
245
How to Use the Object Browser
Objects Members
Members
Objects
Pane Pane
Pane
Pane
Description
Description
Pane
Pane
Procedure for Defining a Class
Declare Properties
Public
Public Sub
Sub TestIt(ByVal
TestIt(ByVal xx As
As Integer)
Integer)
...
...
End
End Sub
Sub
Public
Public Function
Function GetIt(
GetIt( )) As
As Integer
Integer
...
...
End
End Function
Function
Declaring Properties
Protected
Protected Overrides
Overrides Sub
Sub Finalize(
Finalize( ))
'Can
'Can close
close connections
connections or
or other
other resources
resources
conn.Close
conn.Close
End
End Sub
Sub
Creating and Destroying Objects
'Declare
‘declare
‘declare but
'Declare but do
don’t
do not
notinstantiate
don’t instantiate
instantiateyet
instantiate yet
yet
yet
Dim
Dim c1
c1 As
As TestClass
TestClass
‘other
'Other
‘other code
'Other code
c1
c1 == New
New TestClass(
TestClass()
TestClass()))
TestClass( ‘instantiate
'Instantiate
‘instantiate now
'Instantiate now
'Declare,
‘declare,
‘declare, instantiate
'Declare, instantiate && initialize
initialize using
using default
default constructor
constructor
Dim c2 As TestClass = New TestClass()
TestClass(
Dim c2 As TestClass = New TestClass( )
TestClass())
'Declare,
‘declare,
‘declare, instantiate
'Declare, instantiate && initialize
initialize using
using default
default constructor
constructor
Dim c3 As New TestClass
TestClass(
Dim c3 As New TestClass(
TestClass ) )
'Declare,
'Declare, instantiate
instantiate && initialize
initialize using
using alternative
alternative constructor
constructor
Dim
Dim c4
c4 As
As New
New TestClass(10)
TestClass(10)
Dim
Dim c5
c5 As
As TestClass
TestClass == New
New TestClass(10)
TestClass(10)
Garbage Collection
What Is Inheritance?
Overriding and Overloading
Inheritance Example
Shadowing
Using the MyBase Keyword
Using the MyClass Keyword
What Is Inheritance?
Dim
Dim xx As
As DerivedClass
DerivedClass == New
New DerivedClass(
DerivedClass( ))
x.Other
x.Other 'Displays
'Displays "Base
"Base Other
Other method
method –– not
not overridable"
overridable"
x.OverrideMethod
x.OverrideMethod 'Displays "Derived OverrideMethod"
'Displays "Derived OverrideMethod"
Shadowing
Dim
Dim xx As
As DerivedClass
DerivedClass == New
New DerivedClass(
DerivedClass( ))
x.Other(
x.Other( ))
Interfaces
Defining Interfaces
Achieving Polymorphism
Defining Interfaces
Polymorphism
Many classes provide the same property or method
A caller does not need to know the type of class the
object is based on
Two Approaches
Interfaces
Class implements members of interface
Same approach as in Visual Basic 6.0
Inheritance
Derived class overrides members of base class
What Is Polymorphism?
CountyTax
CountyTax CityTax
CityTax
CalculateTax(
CalculateTax( )) CalculateTax(
CalculateTax( ))
Working with Classes
SavingsAccount.InterestRate
SavingsAccount.InterestRate == 0.003
0.003
Dim
Dim acct1
acct1 As
As New
New SavingsAccount("Joe
SavingsAccount("Joe Howard",
Howard", 10000)
10000)
MsgBox(acct1.CalculateInterest,
MsgBox(acct1.CalculateInterest, , "Interest for "" && acct1.Name)
, "Interest for acct1.Name)
Using Shared Procedure Members
'Client
'Client code
code
MsgBox(TestClass.GetComputerName(
MsgBox(TestClass.GetComputerName( ))
))
Event Handling
Sub
Sub HandleEvent(ByVal
HandleEvent(ByVal ii As
As Integer)
Integer)
...
...
End Sub
End Sub
RemoveHandler Keyword: Disconnects from Event Source
What Are Delegates?
Delegate
Delegate Function
Function CompareFunc(
CompareFunc( __
ByVal
ByVal xx As
As Integer,
Integer, ByVal
ByVal yy As
As Integer)
Integer) As
As Boolean
Boolean
Classes Structures
Can define data members, Can define data members,
properties, and methods properties, and methods
Supports constructors and No default constructor or
member initialization member initialization
Support Finalize method Do not support Finalize method;
implement IDisposable
Extensible by inheritance Do not support inheritance
Reference type Value type
Review
Defining Classes
Creating and Destroying Objects
Inheritance
Interfaces
Working with Classes