Application wide protected integer...

Merrion

Junior Contributor
Joined
Sep 29, 2001
Messages
265
Location
Dublin, Ireland
Alright - a bit of thinking outside the box needed here - I want an application wide number that I can increment every time I create a new isnatnce of a particular class. It needs to be thread-safe as well - any thoughts?

Merci en avance,
Le Duncan
 
You could make the variable a publically Shared/static one of the class you are talking about (meaning, of course, that you can access it without needing an instance of the class). Then you just increase it in the constructor and decrease it in the destructor. Much like the way COM classes work with AddRef and Release.

Visual Basic:
Public Class MyClass
  Public Shared refCount As Integer

  Public Sub New()
    refCount += 1
  End Sub

  Protected Overrides Sub Finalize()
    MyBase.Finalize()
    refCount -= 1
  End Sub
End Class
 
Back
Top