0

If I have a class with two base classes :

public partial MyClass : Base1, Base2 {


}

To call the constructor of Base1 I would do this:

public MyClass() : base(myParamForBase1); 

But I need to call the second base class to get the base init value like this:

base.OnInit(e); 

I cannot do the above of course because C# thinks I'm referring to Base1 not Base2, how do I resolve this? In other words how can I refer to Base2?

halfer
  • 19,824
  • 17
  • 99
  • 186
anpatel
  • 1,952
  • 4
  • 19
  • 36
  • 3
    You can't have multiple base classes. You can have a single base class and infinite interfaces – T-moty May 27 '13 at 18:47
  • The need for multiple inheritance always results `from` a poorly designed class structure. Post an example of what you need. I'm sure there's a way to structure it so that you don't need multiple inheritance (which does not exist anyways) – Federico Berasategui May 27 '13 at 18:51
  • @HighCore sure, I'm converting right now believe it or not an MVC project to a standard .NET project for SharePoint. I can post my situation and it would be awesome if you could help me out – anpatel May 27 '13 at 18:53
  • WHAT. Oh I see you didn't knew C# has multiple inheritance. You sounded like you were able to compile all of the above. I thought C# changed alot in newer version – ata May 27 '13 at 19:03
  • @Ata lol yes, I apologize for my ignorance – anpatel May 27 '13 at 19:06

2 Answers2

5

C# does not support multiple class inheritance. You can only implement multiple interfaces, and inherit from (extend) a single base class.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • I was afraid this would be the answer. I saw this multiple times, but just wanted to hold onto hope – anpatel May 27 '13 at 18:52
  • 1
    @MyName, you might want to look at [the diamond problem](http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem) for a reason why this would never work in c#. – gunr2171 May 27 '13 at 19:06
  • @MyName You can, however, work with cascading inheritance. `Grandchild` inherits from `Child`, which inhertis from `Mother`. – Andre Calil May 27 '13 at 19:09
  • @AndreCalil :( I tried the cascading inheritance first, but I had to make them one because I couldn't find a way to make multiple Page_Load events work. – anpatel May 27 '13 at 19:15
  • 1
    @gunr2171 ty, here's a better link though for noobs like myself: http://nazar-merza.com/index.php/getting-started/74-the-problem-with-multiple-inheritance , it explains the diamond problem really well, the animal example I don't think was the best one but still a good resource – anpatel May 27 '13 at 20:00
1

What you can do is, make a class that inherits from the first base

something like this

    public Base2 : Base1 {


    } 
    public MyClass : Base2  {


    }

Not that i like this, but it may help in some cases

CMS
  • 3,657
  • 1
  • 27
  • 46