1 Working with Objects
Jan 05, 2016
1
Working with Objects
2
Defining a Class in VB .NET
• A class is a user-defined data type• You can declare it as part of a module, but usually in a s
eparate file (also .vb)• Ingredients
– Data members (fields)– Constructors– Properties– Methods
• Access Modifiers– Public: accessible by all– Private: accessible by the class itself only– Protected: accessible by the class itself or its sub-classes– Friend: accessible by all in the current assembly
3
ClassExample1 (Horse.vb)Public Class Horse Public m_name As String ‘ data members (fields) Public m_color As String Friend m_height As Single = 1.5 ' in meters Public Sub New() ‘ default constructors MyClass.New("", "", 0) ‘ MyClass (i.e. this in Java) End Sub
Public Sub New(ByVal name As String, ByVal color As String) MyClass.New(name, color, 0) End Sub
Private Sub New(ByVal name As String, ByVal color As String, ByVal height As Single) m_name = name m_color = color If 1 <= height And height <= 2.5 Then m_height = height End If End SubEnd Class
4
ClassExample1 (Module1.vb)
Module Module1 Sub Main() Dim h As Horse = New Horse("Lightning", "Brown") Console.Out.WriteLine("Name is {0}", h.m_name) Console.Out.WriteLine("Color is {0}", h.m_color) Console.Out.WriteLine("Height is {0}", h.m_height) End Sub End Module
5
Properties
• If data members are public, they can be altered directly (horse.m_name), violates Data Hiding principle (Encapsulation)
• If private, need accessor methods (set_ & get_), so Properties are introduced
• Fields and properties are similar, but properties are accessed through its accessor methods Get and Set
• Normally use private or protected fields to hold the data
6
Defining PropertiesPublic Class Horse Public m_name As String Public m_color As String Private m_height As Single = 1.5 Public Property height() As Single Get ‘ Get block Return m_height End Get Set ‘ Set block, note the keyword Value If 1 <= Value And Value <= 2.5 Then m_height = Value End If End Set End Property Public Sub New(ByVal name As String, ByVal color As String, ByVal height As Single) m_name = name m_color = color MyClass.height = height ‘ MyClass.height Is a Property!! End SubEnd Class
7
Using Properties
Module Module1 Sub Main() Dim h As Horse = New Horse("Lightning", "Brown“, 2)
h.height = 1.9 Console.Out.WriteLine("Name is {0}", h.m_name) Console.Out.WriteLine("Color is {0}", h.m_color) Console.Out.WriteLine("Height is {0}", h.height) End Sub End Module
8
ReadOnly Properties• With Get block only• Does not allow changes
Public Class Person Private m_name As String Private m_dob As Date Private m_balances() As Decimal Public ReadOnly Property name() As String Get Return m_name End Get End Property Public ReadOnly Property dob() As Date Get Return m_dob End Get End Property… End Class
9
WriteOnly Properties• With Set block only, rarely used
Public Class OutTextStream Public WriteOnly Property outText() As String Set (ByVal Value As String) Console.Out.WriteLine(Value) End Set End PropertyEnd Class
Module Module1
Sub Main() Dim o As OutTextStream = New OutTextStream() o.outText = “This goes to the console!” End Sub End Module
10
Parameterized Properties• Property “Array”, can be multi-dimensional too
Public Class Person Private m_name As String Private m_dob As Date Private m_balances() As Decimal … Public Property balances(ByVal index As Integer) As Decimal Get Return m_balances(index) End Get Set m_balances(index) = Value End Set End Property Public ReadOnly Property noOfBalances() As Integer Get Return m_balances.Length End Get End Property Public Sub New(ByVal name As String, ByVal dob As Date) m_name = name m_dob = dob m_balances = New Decimal(5) {} End SubEnd Class
‘ Using itDim p As Person = New person("John G.", #7/4/1960#)p.balances(0) = 2100p.balances(1) = 10000
11
ClassExample2
• Lab 7
12
Methods (1)• Behavior, operations that an object can do• A function or subroutine defined inside the class body that can be ca
lled on an object• Can have modifiers Public, Private, Protected, or Friend
Public Class Horse Private m_name As String Private m_color As String Private m_height As Single … ' We assume that a horse can jump 75% of its height Public Function Jump() As Single Return CSng(0.75 * m_height) End FunctionEnd Class
13
Methods (2)‘ Calling Method
Sub Main()
Dim h As Horse = New Horse("Bucefalus", "black", 2)
Console.Out.WriteLine("The horse (0) jumps {1} meters!", _
h.name, h.Jump())
End Sub
• Example ClassExample3
14
Method Overloading (1)• To achieve the same task but with different arguments• E.g. a Add Function that can be applied to Short, Integer, and Long• Must have different argument signatures
Public Class Calculator Public Overloads Function Add(ByVal x As Short, ByVal y As Short) As Long Return x + y End Function Public Overloads Function Add(ByVal x As Integer, ByVal y As Integer) As Long Return x + y End Function Public Overloads Function Add(ByVal x As Long, ByVal y As Long) As Long Return x + y End FunctionEnd Class
15
Method Overloading (2)Module Module1
Sub Main() Dim result As Long Dim calc As New Calculator() Dim s1 As Short = 3, s2 As Short = 5 result = calc.Add(s1, s2) ‘ using Add(Short, Short) Dim i1 As Integer = 3, i2 As Integer = 5 result = calc.Add(i1, i2) ‘ using Add(Integer, Integer) Dim j1 As Long = 3, j2 As Long = 5 result = calc.Add(j1, j2) ‘ using Add(Long, Long) End Sub End Module
• Example ClassExample4
16
ClassExample5Account
id: String
balance: Decimal
New(id:String, balance:Decimal)
Person
name: String
accounts: Account()
balance: Decimal
New(name:String)
Deposit(ac:String, amt:Decimal)
Withdraw(ac:String, amt:Decimal): Boolean
Transfer(acFrom:String, acTo:String, amt:Decimal)
GetAccountById(ac:String): Account
17
Shared Class Members
• Similar concepts of “static” in other OO languages
• Shared data member
• Shared Methods
18
Shared Data Members (1)
• Can be fields or properties• Belong to the class, shared by all instances• Also known as class variable• All instances get the same value• Shared property CANNOT access non-shared (i
nstance) fields or properties• Accessed through class name, e.g. Product.tax
Rate• Save memory• Define constants (no instantiation of class need)
19
Shared Data Members (2)Public Class Product Private m_name As String ' product name (instance variable) Private m_listPrice As Decimal ' the list price (instance variable) Private Shared s_taxRate As Double ‘ shared variable Public Shared Property taxRate() As Double ‘ shared property Get Return s_taxRate End Get Set s_taxRate = Value End Set End Property Public Sub New(ByVal name As String, ByVal listPrice As Decimal) m_name = name m_listPrice = listPrice End Sub ... End Class
20
Shared Methods (1)
• Actions performed on class level, not object level
• Do not require the existence of an object (no instantiation need)
• E.g. some math functions• CANNOT access non-shared (instance)
members• Accessed through class name, e.g.
Product.GetUnknownPrice(10)
21
Shared Methods (2)Public Class Product Private m_name As String ' product name Private m_listPrice As Decimal ' the list price Private Shared s_taxRate As Double ...
Public Sub New(ByVal name As String, ByVal listPrice As Decimal) m_name = name m_listPrice = listPrice End Sub
Public Function GetPrice(ByVal discount As Double) As Decimal Dim price As Double price = m_listPrice + m_listPrice * s_taxRate - m_listPrice * discount Return CDec(price) End Function
Public Shared Function GetPriceUnknown(ByVal price As Decimal) As Decimal Return CDec(price + price * s_taxRate) End Function
End Class
22
ClassExample6