SIGN UP MEMBER LOGIN:    
ARTICLE

Object Creation and Instantiation

Posted by Mahadesh Mahalingappa Articles | Visual C# July 22, 2011
In this article I am trying an alternative approach for Multiple Inheritance.
Reader Level:

In this article I am trying an alternative approach for Multiple Inheritance.

In C sharp Multiple Inheritance is not possible to achieve this is well known. i.e. You cannot inherit from two classes.

Program : ClassA , ClassB // This cannot be achieved
{

}

So lets try an alternative approach where we can leverage the advantages of Multiple Inheritance . Please note that I have not used Reflection in this example , that is not the scope of this article .


Consider ClassA and ClassB as the two classes whose functionality we need to access .

  class ClassA
    {
        public string member { get; set; }
        public ClassA()
        {
            Console.WriteLine("A constructor is called");
            member = "Member of ClassA";
        }
        public void display()
        {
            Console.WriteLine("Display A");
        }
    }

class ClassB
    {
        public string member { get; set; }
        public ClassB()
        {
            Console.WriteLine("B constructor is called");
            member = "Member of ClassB";
        }
        public void display()
        {
            Console.WriteLine("Display B");
        }
    }

Creating a Router :


Let us create a Router Interface .

   interface IRouter
    {
        void display(string param);
    }



The Router class looks as follows :

class Router : IRouter
    {
        public string member { get; set; }

         public Router(string param)
        {
            if (param == "A")
            {
                new ClassA();
                member = new ClassA().member;
            }
            else if (param == "B")
            {
                new ClassB();
                member = new ClassB().member;
            }
        }

        public void display(string param)
        {
            if (param == "A")
            {
                new ClassA().display();
            }
            else if (param == "B")
            {
                new ClassB().display();
            }
        }
    }

  class Program
    {
        static void Main(string[] args)
        {
            IRouter router = new Router("A");
            router.display("B");
            Console.Read();
        }
    }



The following output is displayed :

routers

Conclusion

This is a simple Router which routes the method calls to Respective classes based on the User Input. Hopefully it would be useful. Thanks .

Login to add your contents and source code to this article
share this article :
post comment
 

No this is definitely not inheritance. this is just a alternative where if we have to use multiple inheritance in some case we might think of some common class which would forward the request to another classes classA and classB in this case. This way i can access the functionality of other classes in my Program class. Hence i can actually get the advantages of Multiple inheritance . Please let me know if it makes sense.

Posted by Mahadesh Mahalingappa Jul 22, 2011

Does this make the syntax for using the Router class the same as if the Router class inherited from two classes? What are the two classes; are they ClassA and ClassB?

Posted by Sam Hobbs Jul 22, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor