An Introduction To Object Orientated Programming with VB4/5/6
Introduction
This is the first in a series of Object Orientated Programming (OOP) tutorials using Visual Basic. This first part introduces you to the concepts behind OOP and explains objects as they are used in Visual Basic.
Definition of an Object
It seems that there is no one definition of an object that developers and designers can agree on. Possibly the one that is nearest to the mark is :
(From Jacobson: Object Orientated Software Engineering)
That's all very well, but what does that mean to a VB Programmer ? If you've ever written a Visual Basic program with a form or controls, then you've already had a quick taste of objects. Consider a Visual Basic Form - a form is an object that VB knows about. Once a form is running in your program, it is able to :
An object is supposed to offer a programmer or another program, an Application Programming Interface (API). For an object, this consists of :
A class module is simply a description or a 'blueprint' of an Object. For example :
- These could both be called Instances of the class Remember
Let's consider how we can perceive a real-world object.
When I get into my car in the morning to drive to work, I place my key in the ignition to start the engine and (hopefully) drive to work carefree. I don't need to know about the internal combustion engine to drive my car, nor do I need to understand how the wiring works in order to operate my lights and indicators. The car can be said to provide an Interface that I need to know about (and a separate interface for those brave enough to look under the bonnet). My car can be though of as an object that offers properties (current speed, fuel level, oil level etc) and methods (start-engine, stop engine etc).
A class representing an object should :
Now that you have a better understanding of the concepts behind Object Orientated Programming, let's move onto a Visual Basic example. Since VB4, developers have been able to add class modules to their projects. While a lot of people assume that just adding a class to your program makes it Object Orientated, the program is doomed to failure unless there is a good design for the system.
For our VB example, we'll take a 'Bank Account' real world object and create a Visual Basic class that models it's real-world behaviour.
When we think about a Bank Account, we think of it as having :
Hopefully, you'll have read through the above list and already identified what are the properties and methods of the class module we are about to create. Let's jump straight into the Visual Basic IDE and start creating our project.
Create a standard VB EXE project, call it prjBankAccount1 . Next, add a class module to the project and call it cBankAccount .
The cBankAccount class is listed below - notice how when the 'cBankAccount' object is instantiated (ie. Set o = New cBankAccount), the class uses it's own internal properties to set the internal varables. This ensures that errors are caught right from the start.
The class module will be expanded upon in the next part of this
Definition of a Class Module
Thinking about objects
Signs of a 'good' Object
Real World VB Object Programming
A good design comes from a good understanding of the problem / project domain - you need to fully understand :
option Explicit
'
' Bank Account Class
'
private mdblBalance as Double
private mdblInterestRate as Double
private mdblMonthlyFee as Double
'
private Sub Class_Initialize()
'
' Class Initialize is the first routine
' called when you create a 'cBankAccount'
' object.
'
' Eg. set oBA = new cBankAccount
'
mdblBalance = 0
me.InterestRate = 1.75
me.MonthlyFee = 5
'
End Sub
'
private Sub Class_Terminate()
'
' Class_Terminate is called when you
' set the object to nothing (or it falls out of scope)
'
End Sub
'
public property get Balance() as Double
'
' Return the Balance of this account
'
Balance = mdblBalance
'
End property
'
public property get InterestRate() as Double
'
' Return the interest rate
'
InterestRate = mdblInterestRate
'
End property
'
public property let InterestRate(byval dblNewInterestRate as Double)
'
' Check for invalid interest rates
'
If dblNewInterestRate < 0 then
Err.Raise vbObjectError + 2001, _
"cBankAccount::InterestRate", _
"Invalid Interest Rate set : " & dblNewInterestRate
End If
'
' Now set the rate
'
mdblInterestRate = dblNewInterestRate
End property
'
public property let MonthlyFee(byval dblNewFee as Double)
'
' Check for invalid Fee
'
If dblNewFee < 0 then
Err.Raise vbObjectError + 2003, _
"cBankAccount::MonthlyFee", _
"Invalid Fee Amount : " & dblNewFee
End If
'
mdblMonthlyFee = dblNewFee
'
End property
'
public Sub MonthlyProcessing()
'
' This routine takes the monthly fee
' from the account, then updates the
' balance with interest earned.
'
CalcFee
CalcInterest
'
End Sub
'
public Sub Withdraw(byval dblAmount as Double)
'
' Withdraw some money from the account
'
' Sanity check first !
'
If dblAmount <= 0 then
Err.Raise vbObjectError + 2002, _
"cBankAccount::Withdraw", _
"Invalid Withdraw amount : " & dblAmount
End If
If (mdblBalance - dblAmount) < 0 then
Err.Raise vbObjectError + 2003, _
"cBankAccount::Withdraw", "Insufficient Funds"
End If
mdblBalance = mdblBalance - dblAmount
End Sub
'
public Sub Deposit(byval dblAmount as Double)
'
' Deposit some money to the account
'
' Sanity check first
'
If dblAmount <= 0 then
Err.Raise vbObjectError + 2002, _
"cBankAccount::Deposit", _
"Invalid deposit amount : " & dblAmount
End If
'
mdblBalance = mdblBalance + dblAmount
'
End Sub
'
private Sub CalcFee()
'
' This routine removes the monthly fee
' from the balance
'
mdblBalance = mdblBalance - mdblMonthlyFee
'
End Sub
'
private Sub CalcInterest()
'
' This is private, as we don't want
' other programs / programmers updating
' the balance except through the
' proper interfaces
'
' The calculation updates the balance with the monthly interest
' due
'
mdblBalance = mdblBalance + _
(mdblBalance * (mdblInterestRate / 120))
'
End Sub
'
'
tutorial when we'll cover another object orientated concept -
polymorphism.

Comments
coplete learning of vb
Posted by Legacy on 09/12/2003 12:00amOriginally posted by: nithya
ReplyObject oriented modeling
Posted by Legacy on 08/12/2003 12:00amOriginally posted by: navneet
ReplyDatabase connectivity
Posted by Legacy on 11/12/2002 12:00amOriginally posted by: Bijukutty
Please send me an article on how to connect databse to VB.
ReplyPlease send me the article about polymorphism.
Posted by Legacy on 11/11/2002 12:00amOriginally posted by: Vivek
Hello
Your article contains below mentioned line,so it would be great if u can send me the below mentioned article."The article on Polymorphism."
Thanks
regards
Vivek
P.S.-This is the article what i would like to have.
"The class module will be expanded upon in the next part of this
tutorial when we'll cover another object orientated concept -
polymorphism. "
ReplyAbout Exitfunction
Posted by Legacy on 03/18/2001 12:00amOriginally posted by: Hanmant Renushe
Sir
Replyi always refer the code with this site , i used the function j=exitwindows(i,0) but it show error for exitwindows funtion so what should i do
please send your reply imediately
Thank you
I Can't Download
Posted by Legacy on 01/27/2001 12:00amOriginally posted by: SANTI
Dear sir
I can't download Zip code.
Reply