It is simpler than that. You need a static field as counter, and be sure to make all constructors increment it, and the destructor decrement it (be aware of the garbage collector) - but this later part is not needed in you case as I see.
The c# template is the follwoing:
class MyClass
{
static int counter = 0;
public MyClass()
{
Interlocked.Increment(ref counter);
}
public ~MyClass()
{
Interlocked.Decrement(ref counter);
}
}
The VB.NET version would be:
Class [MyClass]
Shared counter As Integer = 0
Public Sub New()
Interlocked.Increment(counter)
End Sub
Protected Overrides Sub Finalize()
Try
Interlocked.Decrement(counter)
Finally
MyBase.Finalize()
End Try
End Sub
End Class
The list part is a little bit more complicated because of the same garbage collector (
source[
^])
public class MyClass
{
private static List<WeakReference> instances = new List<WeakReference>();
public MyClass()
{
instances.Add(new WeakReference(this));
}
public static IList<MyClass> GetInstances()
{
List<MyClass> realInstances = new List<MyClass>();
List<WeakReference> toDelete = new List<WeakReference>();
foreach(WeakReference reference in instances)
{
if(reference.IsAlive)
{
realInstances.Add((MyClass)reference.Target);
}
else
{
toDelete.Add(reference);
}
}
foreach(WeakReference reference in toDelete) instances.Remove(reference);
return realInstances;
}
}
But can be simpler if the usage of the list let's you make it simpler.
[Update: working exampe]
using System;
using System.Collections.Generic;
using System.Linq;
namespace listed
{
public class MyClass
{
#region instance collection
private static List<WeakReference> instances = new List<WeakReference>();
public IList<WeakReference> Instances
{
get
{
return instances.AsReadOnly();
}
}
public static IList<MyClass> GetInstances()
{
List<MyClass> realInstances = new List<MyClass>();
List<WeakReference> toDelete = new List<WeakReference>();
foreach(WeakReference reference in instances)
{
if(reference.IsAlive)
{
realInstances.Add((MyClass)reference.Target);
}
else
{
toDelete.Add(reference);
}
}
foreach(WeakReference reference in toDelete) instances.Remove(reference);
return realInstances;
}
#endregion
#region instance specific
public int instanceIndex { get; private set; }
public string baseTitle { get; private set; }
public string Title
{
get
{
return string.Format("{0}{1}", this.baseTitle, this.instanceIndex);
}
}
public MyClass(string title)
{
this.baseTitle = title;
this.instanceIndex = instances.Count(x => ((MyClass)x.Target).baseTitle == title) + 1;
instances.Add(new WeakReference(this));
}
#endregion
}
class Program
{
public static void Main(string[] args)
{
var o1 = new MyClass("List");
Console.WriteLine(o1.Title);
var o2 = new MyClass("SampleList");
Console.WriteLine(o2.Title);
var o3 = new MyClass("List");
Console.WriteLine(o3.Title);
var o4 = new MyClass("SampleList");
Console.WriteLine(o4.Title);
var o5 = new MyClass("List");
Console.WriteLine(o5.Title);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}