Classes and Objects
A class is a blueprint that bundles data (fields and
properties) with behavior (methods). An object is one
instance created from it with New.
Public Class BankAccount
Public Owner As String
Private balance As Decimal
Public Sub Deposit(amount As Decimal)
If amount <= 0 Then Throw New ArgumentException("Amount must be positive.")
balance += amount
End Sub
Public Function Withdraw(amount As Decimal) As Boolean
If amount > balance Then Return False
balance -= amount
Return True
End Function
Public Function GetBalance() As Decimal
Return balance
End Function
End Class
Module Program
Sub Main()
Dim account As New BankAccount()
account.Owner = "Ada"
account.Deposit(100D)
Console.WriteLine(account.Withdraw(30D))
Console.WriteLine(account.Withdraw(500D))
Console.WriteLine($"{account.Owner}: {account.GetBalance()}")
End Sub
End Module
True
False
Ada: 70
Dim account As New BankAccount()declares a variable and creates the object in one step (the same asDim account = New BankAccount()).Privatemembers are hidden from code outside the class — here,balancecan only change throughDepositandWithdraw. This is encapsulation.- Methods are just
Subs andFunctions inside a class.
Objects are references
A class variable holds a reference to an object. Assigning it copies the reference, not the object:
Public Class Counter
Public Value As Integer
End Class
Module Program
Sub Main()
Dim a As New Counter()
Dim b = a
b.Value = 5
Console.WriteLine(a.Value)
Console.WriteLine(a Is b)
Dim c As New Counter() With {.Value = 5}
Console.WriteLine(a Is c)
End Sub
End Module
5
True
False
a and b point at the same object, so changing it through b shows
up in a. c is a different object even though its value is the same.
With {.Value = 5} is an object initializer that sets members right
after creating the object.
Me
Inside a class, Me refers to the current object — useful when a
parameter has the same name as a field:
Public Class Dog
Private name As String
Public Sub Rename(name As String)
Me.name = name
End Sub
Public Function Speak() As String
Return $"{Me.name} says woof"
End Function
End Class
Module Program
Sub Main()
Dim d As New Dog()
d.Rename("Rex")
Console.WriteLine(d.Speak())
End Sub
End Module
Rex says woof
Nothing
A class variable that doesn't point to any object is Nothing. Calling a
member on it throws a NullReferenceException:
Public Class Dog
Public Name As String = "Rex"
End Class
Module Program
Sub Main()
Dim d As Dog = Nothing
Console.WriteLine(d Is Nothing)
Console.WriteLine(d?.Name)
Console.WriteLine(d.Name)
End Sub
End Module
True
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Demo.Program.Main()
d?.Name returns Nothing (printed as an empty line) instead of
crashing; d.Name throws.
Practice
Write a Rectangle class with public Width and Height fields and
methods Area() and Perimeter(). Create one with an object
initializer and print both values.
Show solution
Public Class Rectangle
Public Width As Double
Public Height As Double
Public Function Area() As Double
Return Width * Height
End Function
Public Function Perimeter() As Double
Return 2 * (Width + Height)
End Function
End Class
Module Program
Sub Main()
Dim r As New Rectangle() With {.Width = 3, .Height = 4.5}
Console.WriteLine($"Area {r.Area()}, perimeter {r.Perimeter()}")
End Sub
End Module
Area 13.5, perimeter 15