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.

Have you thought of making the myproj classes into subclasses of the tasklayer class set?

Doing something along the lines of:

public class _C : Tasklayer

An example of setting up inheritance can be found at THIS LINK.

Hope this helps solve your problem :) If it does, please mark thread as solved.

You could add a static auto creating property (or method) in each of the TaskLayer classes.
I use this strategy quit a lot when I want a common shared instance of a class.

namespace TaskLayer
{
    public class A
    {
        private static A _default;
        public static A Default
        {
            get
            {
                if (_default == null)
                    _default = new A();
                return _default;
            }
        }
    }
}

namespace MyProj
{
    public class _A
    {
        TaskLayer.A a = TaskLayer.A.Default;
    }
}
commented: Great answer! +6

Great example of SingleTon class by nick.crane. Here is MSDN article - Implementing Singleton in C#.

SUMMARY:
You are building an application in C#. You need a class that has only one instance, and you need to provide a global point of access to the instance. You want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET common language runtime features. You may also want to make sure that your solution is thread safe.

commented: Nice observation :) +6

@Lushipur
I cannot inherit the tasklayer classes

@Nick,

I cannot touch the Tasklayer.
Is there any other way I can implement this?

PLease help.

Sorry, was trying to do what I could with what was provided but did you look into adatapost's suggestion at all? I had completely forgotten about that option when I was posting my original reply :)

Sorry, I am kind of new to this Singleton concept.
Can you implement this in the code in the original post and let me know how it is done?


Thanks
Ananya

The static "Singleton" code does not have to be in the TaskLayer project or even in the same namespace. As long as you can create a TaskLayer.A object then you can put it anywhere.
I often use a static Tools class to hold global (or project-wide) static properties and methods.

namespace MyProj
{
    public static class Tools
    {
        private static TaskLayer.A _defaultA;
        public static TaskLayer.A GetDefaultA
        {
            get
            {
                if (_defaultA == null)
                    _defaultA = new TaskLayer.A();
                return _defaultA;
            }
        }
    }

    public class _A
    {
        TaskLayer.A a = Tools.GetDefaultA;
    }
}

Hi dreamy_ananya,

Based on the scenario you described, a singleton is not really a good solution. A singleton is essentially a global instance (or several global instances) of a class. It shouldn't be used just to provide lookup for a certain class -- that is not its intended purpose and will come back to bite you later on.

I would recommend the following:

// _A.cs
class _A
{

    private TaskLayerController m_TLC= null;

    public _A(TaskLayerController tlc)
    {
        m_TLC= tlc;
    }

}

.. Repeat for B, C

Now, create a new class

// In TaskLayerController.cs
class TaskLayerController 
{
   A a = null;
// Repeat for B, C

   public TaskLayerController(..some parameters..)
   {
     a = new A(..parameters....);
     // repeat for B, C
   }

   public ChangeSomethingInABC(..params..)
   {
    a.whatever(params);
   }

   public B B { get { return B; } } // if you really have to get the class itself for some reason

}

Now, somewhere in your main load or application entry point

private TaskLayerController m_TLC = new TaskLayerController(...params...);

Now you have an instance (m_TLC) in your main class which wraps all of your classes related to Tasklayer to fit the needs of your own application! The TaskLayerController is the only part of your application that understands how to interact with Tasklayer classes, and provides an interface for everyone else.


Hope this helps!

Vasiliy Deych

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.