Module Tester
Sub Main()
Console.WriteLine("Employees before instantiation: " & _
Employee.Count)
Dim employee1 As Employee = New Employee("S", "B")
Dim employee2 As Employee = New Employee("B", "J")
Console.WriteLine("Employee.Count: " & Employee.Count)
employee1 = Nothing
employee2 = Nothing
System.GC.Collect() ' request garbage collection
Console.WriteLine("Employee.Count: " & Employee.Count)
End Sub ' Main
End Module
Class Employee
Inherits Object
Private mFirstName As String
Private mLastName As String
Private Shared mCount As Integer
Public Sub New(ByVal firstNameValue As String, _
ByVal lastNameValue As String)
mFirstName = firstNameValue
mLastName = lastNameValue
mCount += 1
Console.WriteLine _
("Employee object constructor: " & mFirstName & _
" " & mLastName)
End Sub ' New
Protected Overrides Sub Finalize()
mCount -= 1 ' decrement mCount, resulting in one fewer object
Console.WriteLine _
("Employee object finalizer: " & mFirstName & _
" " & mLastName & "; count = " & mCount)
End Sub ' Finalize
Public ReadOnly Property FirstName() As String
Get
Return mFirstName
End Get
End Property ' FirstName
' return last name
Public ReadOnly Property LastName() As String
Get
Return mLastName
End Get
End Property ' LastName
' property Count
Public Shared ReadOnly Property Count() As Integer
Get
Return mCount
End Get
End Property ' Count
End Class
|