Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

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

Printer Monitor for .NET? - see Merrion Computing Ltd for details
  • *Experts*
Posted

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.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...