mrdance Posted November 25, 2003 Posted November 25, 2003 (edited) Is there an easy way to get number of objects created from a class? Thanks / Henrik Edited November 25, 2003 by mrdance Quote
kejpa Posted November 25, 2003 Posted November 25, 2003 You could have a global variable that you increment everytime you create a new object from the specific class. /Kejpa Quote
Administrators PlausiblyDamp Posted November 25, 2003 Administrators Posted November 25, 2003 You could implement a reference counting scheme in the class itself. Add a shared (vb) or static (C#) variable to the class and in the constructor (Sub New) increment it and in the Desctructor (Finalize method) decrement it. The shared variable will then be the number of objects created that haven't yet been garbage collected. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrdance Posted November 25, 2003 Author Posted November 25, 2003 Sounds nice but it never reaches finalize. I am listening to a tcp socket. Right now, after your answer, I call finalize when I receive an error on the socket. In finalize I decrease the activeConnections variable. It seems to work ok but it bothers me that it never reaches finalize by itself. Thanks / Henrik Quote
Administrators PlausiblyDamp Posted November 25, 2003 Administrators Posted November 25, 2003 Finalize will be called sooner or later - however garbage collection is lazy by design. If you want more control search in MSDN for info on IDisposable. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.