Professional Documents
Culture Documents
Module 5: Object-Oriented Programming
Module 5: Object-Oriented Programming
Oriented Programming
in Visual Basic .NET
Overview
Defining Classes
Creating and Destroying Objects
Inheritance
Interfaces
Working with Classes
Defining Classes
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 butbut do
do not
not instantiate
instantiate yet
yet
Dim
Dim c1
c1 As
As TestClass
TestClass
'Other
'Other code
code
c1
c1 == New
New TestClass(
TestClass( )) 'Instantiate
'Instantiate now
now
'Declare,
'Declare, instantiate
instantiate && initialize
initialize using
using default
default constructor
constructor
Dim c2 As TestClass = New TestClass(
Dim c2 As TestClass = New TestClass( ) )
'Declare,
'Declare, instantiate
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( ) 'Displays "Derived OverrideMethod"
x.OverrideMethod( ) 'Displays "Derived OverrideMethod"
Shadowing
Dim
Dim xx As
As DerivedClass
DerivedClass == New
New DerivedClass(
DerivedClass( ))
x.Other(
x.Other( ))
Demonstration: Inheritance
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
Demonstration: Interfaces and Polymorphism
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
'TestClass
'TestClass code
code
Public
Public Shared
Shared Function
Function GetComputerName(
GetComputerName( )) As
As String
String
...
...
End
End Function
Function
'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
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
Support constructors and No default constructor or
member initialization member initialization
Support Finalize method Do not support Finalize method
Extensible by inheritance Do not support inheritance
Reference type Value type
Lab 5.2: Inheriting the Package Class
Review
Defining Classes
Creating and Destroying Objects
Inheritance
Interfaces
Working with Classes