Simple Classes - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Simple Classes

Description:

An object is a functional component in a program. A class is the blueprint for a class. ... Although the odometer reading could have been a property. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 14
Provided by: davidlk2
Category:

less

Transcript and Presenter's Notes

Title: Simple Classes


1
Simple Classes
  • The key ingredient of object oriented programming
    (OOP)

2
Classes 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.
3
Classes 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
4
Simple 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.
5
Class 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.
6
Class 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.
7
Class 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
8
Illustrative 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
9
Illustrative 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.
10
Illustrative 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.
11
Illustrative 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.
12
Illustrative 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
13
Illustrative Application
'This method returns the price times 1.2
Function CostToCustomer() Return
CStr(CInt(strPrice) 1.2) End Function
Write a Comment
User Comments (0)
About PowerShow.com