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
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
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter