Click here to Skip to main content
16,017,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a project that has 11 classes. Now each class refers to the tasklayer ( another set of multiple classes) and instantiates them everytime.

For ex:

TaskLayer: contains 3 classes under same namespace.
A.cs
------
public class A{}


B.cs
---------
public class B{}


C.cs
--------
public class C{}



MyProj: my classes needs to refer to tasklayer classes
_A.cs
------
using Tasklayer;
class _A
{
A a = new A();
B b = new B();
C c = new C();
}


_B.cs
-----
using Tasklayer;
class _B
{
A a = new A();
B b = new B();
C c = new C();
}


_C.cs
------
using Tasklayer;
class _C
{
A a = new A();
B b = new B();
C c = new C();
}


Now I dont wanna create a separate set of objects for each of these class in myproj.
I want to create a single of set of object of the tasklayer classes and referance those in my myproj classes.

Can you please tell me how to do that.
Posted
Updated 2-Jul-10 9:52am
v2

Hope this will Helps you


C#
public class A
  {
  }
  public class B
  {
  }
  public class C
  {
  }


  public class Common
  {
    private static A aInstance = new A();
    private static B bInstance = new B();
    private static C cInstance = new C();

    public static A AInstance
    {
      get { return Common.aInstance; }
      set { Common.aInstance = value; }
    }

    public static B BInstance
    {
      get { return Common.bInstance; }
      set { Common.bInstance = value; }
    }

    public static C CInstance
    {
      get { return Common.cInstance; }
      set { Common.cInstance = value; }
    }
   }

  public class _A
  {
    A a = Common.AInstance;
    B b = Common.BInstance;
    C c = Common.CInstance;
  }
 
Share this answer
 
v3
Create a singleton class and access it using the same method (say getSingleton()) method. This should give you access to the same object in various different classes.
 
Share this answer
 
Comments
ananya choudhury 4-Jul-10 9:14am    
Hi Abhanav,
Can you implement in a sample code, as shown above?

I am very new to this concept, so it would be great if I can see an example.

Thanks
Abhinav S 5-Jul-10 1:34am    
One sample here - http://msdn.microsoft.com/en-us/library/ff650316.aspx
why dont you try creating a static helper class which would contain a method which can return you the instance of the class which you want to refer.
 
Share this answer
 
Hi Chetan,

Using public static, will only help to call the objects with the class name like
CommonInstance.a.FooFunction(a,a,a);

Here, CommonInstance.a in class _A is not pointing to the same instance as by CommonInstance.a in class _B.


My requirement is to call the same instance in multiple classes, and not create different copies.
 
Share this answer
 
Comments
Toli Cuturicu 3-Jul-10 16:19pm    
Reason for my vote of 1
fake answer
Hi ananya,
"public static" is one of the solution for this...

C#
using tasklayer;
namespace MyProj
{
   public static class CommonInstance
   {
      public static A a = new A();
      public static B b = new B();
      public static C c = new C();
   }
   class _A
   {
     //Do here whatever you want to do
     CommonInstance.a.FooFunction(a,a,a);
   }
   class _B
   {
     //Do here whatever you want to do
     CommonInstance.a.FooFunction(b,b,b);
   }
   class _C
   {
      //Do here whatever you want to do
      CommonInstance.a.FooFunction(c,c,c);
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900