tags:

views:

106

answers:

3

Hello All

I have two classes ClassA and ClassB both having a reference to a singleton object ClassHelper. My question is how should i dispose the singleton object once im done using both the ClassA and ClassB

Edit:

public ClassA
{
  CHelper obj;

  public ClassA()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncA();
  }
}

On the same lines
public ClassB
{
  CHelper obj;

  public ClassB()
  {
    obj = obj.GetInstance("Initialise");
    obj.CallFuncB();
  }
}


where 

CHelper
{
   private static sm_CHelper;

   public static GetInstance(string strInitialise)
   {
      if(sm_CHelper == null)
      {
         sm_CHelper = new CHelper(strInitialise);
      }
   }

   private CHelper(string strInitialise)
   {
      //do something here 
   }

   public CallFuncA()
   {
     // do something here
   }
   public CallFuncB()
   {
     // do something here
   }
}

Regards Learner

+1  A: 

if you are talking about the pattern singelton then you should not dispose it.... if your not referring to the singelton pattern then you could try to use the deconstructor to run your dispose logic.

Petoj
A: 

That singleton should remain alive for the duration of the application. So you shouldn't dispose of it when disposing of those ClassA and ClassB's.

Hans Kesting
A: 

I've never seen an example like this. I'd probably do something like:

 class Resource {

     static Resource Instance  = new Resource();
     static int count = 2;

     public Resource CheckOut() { 
        if (--count <= 0)
            Instance = null;
        return Instance;
     }
 }

this way, after both ClassA and ClassB checkout the Resource, the static reference stops keeping it around. After ClassA and ClassB lose their reference to the Resource, the finalizer gets called the next round of garbage collection.

Jimmy