
206 Chapter 5 • .NET Programming Fundamentals
Class SomeClass
Private Shared NumInstances As Integer = 0
Public Sub New()
NumInstances = NumInstances + 1
End Sub
Public ReadOnly Property Instances() As Integer
Get
Return NumInstances
End Get
End Property
End Class
Public Sub testShared()
Dim clsSomeClass1 As New SomeClass()
Dim clsSomeClass2 As SomeClass
Dim num As Integer
num = clsSomeClass1.Instances ' returns 1
clsSomeClass2 = New SomeClass()
num = clsSomeClass2.Instances ' returns 2
End Sub
In this example, we created a constructor that increments the NumInstances
variable.When the first class is instantiated, this variable is equal to 1.When the
second class ...