Title: Simple Classes
1Simple Classes
- The key ingredient of object oriented programming
(OOP)
2Classes and Objects
- An object is a functional component in a program.
- A class is the blueprint for a class.
How many original blueprints are needed to build
1, 000,000 Bic pens? Answer 1 Think of the
blueprint as a class. Think of each pen as an
object of that class.
3Classes must be defined.
- Classes are almost always Public.
- They can be used and reused in more than one
application.
Public Class YourClassName Private intProperty
As Integer Sub New() intProperty 0 End
Sub Property Property1 as Integer Get
Return intProperty End Get Set(ByVal
Property1 As Integer) Property1 Value End
Set End Property End Class
4Simple Class Definition
Example Code
Public Class Automobile Private strManufacturer
as String Private strModelName As
String Private strYear As String Private
strEngineHorsePower Private strColor As
String Private strCondition As String Private
strPrice As StringEnd Class
This would go at the bottom of your code listing
for Form1. Put it beneath End Class. Looks like
a structure definition. This definition as it
stands does absolutely nothing.
5Class Constructors
- The definition by itself is useless!
- Every constructor is called "New"
- Your constructor overloads New operator.
Sub New() strManufacturer "" strModelName
"" strYear "" strEngineHorsePower
"" strColor "" strCondition "" strPrice
""End Sub
All of this goes below the definition.
6Class Properties
- You need to add block of code like this for each
defined variable
Property Manufacturer() as String Get
Return strManufacturer End Get Set(By
Val Value as String) strManufacturer
Value End SetEnd Property
Makes property readable
Makes property write able.
7Class Methods
- Although the odometer reading could have been a
property. It is made a method just for
illustration.
Function CostToCustomer() Return
CStr(CInt(strPrice) 1.2) End Function
8Illustrative Application
'OurCar is an instance of the Automobile class
Dim OurCar As New Automobile 'This procedure
assigns values to the OurCar properties. At the
click 'the properties are assigned values in
the TextBoxes. Private Sub btnSetProperties_Cl
ick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSetProperties.Click
With OurCar .Manufacturer
txtManufacturer.Text .ModelName
txtModel.Text .Year txtYear.Text
.Condition txtCondition.Text
.Color txtColor.Text .Price
txtPrice.Text .EngineHorsePower
txtEngineHorsePower.Text End With End
Sub
declares OurCar of type Automobile
all properties defined by Class Automobile
9Illustrative Application
'This procedure recalls the OurCar properties
and displays them in lables Private Sub
btnGetProperties_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnGetProperties.Click With
OurCar lblManufacturer.Text
.Manufacturer lblModel.Text
.ModelName lblYear.Text .Year
lblCondition.Text .Condition
lblColor.Text .Color lblPrice.Text
.Price lblEngineHorsePower.Text
.EngineHorsePower End With End Sub
At the click, the OurCar Properties are displayed
in the labels.
10Illustrative Application
Private Sub btnEngineHorsePower_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnEngineHorsePower.Clic
k lblPrice.Text OurCar.CostToCustomer
End Sub
This procedure invokes the OurCar.CostToCustomer
method. The identifier of the method is like a
variable that holds the string.
11Illustrative Application
'This code could have been placed in a class
module. Public Class Automobile Private
strManufacturer As String Private
strModelName As String Private strYear As
String Private strEngineHorsePower
Private strColor As String Private
strCondition As String Private strPrice As
String
Declare the variables in the class definition.
Each variable will be used to hold the value of a
property.
12Illustrative Application
Property Manufacturer() As String Get
Return strManufacturer End Get
Set(ByVal Value As String)
strManufacturer Value End Set End
Property
For each variable, you need a Get block and a Set
block inside a Property block.
"Get" allows you to read the value of a
property. "Set" allows you to change the value of
a property The variable Value is not declared and
should not be declared.
Read about WriteOnly and ReadOnly in Help
13Illustrative Application
'This method returns the price times 1.2
Function CostToCustomer() Return
CStr(CInt(strPrice) 1.2) End Function