Home  /  Questions  /  Question



50   50
Jul 04, 2010


Objects that can be shared across multiple classes

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 example:
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 don't want to create a separate set of objects for each of these classes in MyProj.
I want to create a single of set of objects of the tasklayer classes and reference these in my MyProj classes.

Can anyone please tell me how to do that?  Can I use Singleton here?  Will the same object of the Singleton class be referred in all of MyProj classes?

Thanks.



50   50
Jul 05, 2010
In MyProj, create a separate shared class  ( Class _D )

Expose properties from which return objects of A,B,C

In constructor of class _D, create objects of A,B,C..

So when class _D gets instantiated, its created objects of A,B, C.

Since _D is shared class, single objects of _D,A,B,C are in scope.

Thanks,
Shailendra
 1 comment
 
Can you please implement it in the sample code above and show me? Thanks Ananya --- Ananya Choudhury  Jul 06, 2010