Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Dark-Feanor on April 15, 2004, 06:46 PM

Title: [C#] Inheriting from multiple classes
Post by: Dark-Feanor on April 15, 2004, 06:46 PM
I am trying to have one class inherit the methods of many classes. The code that I tried was:

public class Test : Test1, Test2, Test3, Test4{

I get a compile error of:
Quote
Test2: type in interface list is not an interface
Test3: type in interface list is not an interface
Test4: type in interface list is not an interface
Thank you for any help that you can offer.
Title: Re:[C#] Inheriting from multiple classes
Post by: MyndFyre on April 15, 2004, 06:52 PM
Quote from: DaRk-FeAnOr on April 15, 2004, 06:46 PM
I am trying to have one class inherit the methods of many classes. The code that I tried was:

public class Test : Test1, Test2, Test3, Test4{

I get a compile error of:
Quote
Test2: type in interface list is not an interface
Test3: type in interface list is not an interface
Test4: type in interface list is not an interface
Thank you for any help that you can offer.

C# does not permit multiple inheritence, just multiple implementation.  1 direct parent class and unlimited interfaces.  If you want to have it "inherit" multiple classes, you have to go:

Test1
-> Test2
----> Test3
-------> Test4
----------> Test

With Test1 deriving from System.Object (implicit).
Title: Re:[C#] Inheriting from multiple classes
Post by: Dark-Feanor on April 15, 2004, 06:55 PM
I see, I will figure out a way around this then. Thanks.
Title: Re:[C#] Inheriting from multiple classes
Post by: Tuberload on April 15, 2004, 07:15 PM
Quote from: DaRk-FeAnOr on April 15, 2004, 06:55 PM
I see, I will figure out a way around this then. Thanks.

Myndfyre just told you the way around the problem...
Title: Re:[C#] Inheriting from multiple classes
Post by: MyndFyre on April 15, 2004, 08:00 PM
Quote from: Tuberload on April 15, 2004, 07:15 PM
Quote from: DaRk-FeAnOr on April 15, 2004, 06:55 PM
I see, I will figure out a way around this then. Thanks.

Myndfyre just told you the way around the problem...

The obvious escapes all but the most brilliant...  ;)
Title: Re:[C#] Inheriting from multiple classes
Post by: Tuberload on April 15, 2004, 08:28 PM
Quote from: Myndfyre on April 15, 2004, 08:00 PMThe obvious escapes all but the most brilliant...  ;)

Sorry for the pointless post, but that was good. ;D I love a good laugh.
Title: Re:[C#] Inheriting from multiple classes
Post by: Dark-Feanor on April 23, 2004, 11:40 AM
I did not feel that the soluction presented by myndfyre was very logical in the way I think about my application running, so I would rather do it a different way.
Title: Re:[C#] Inheriting from multiple classes
Post by: K on April 23, 2004, 01:34 PM
Quote from: DaRk-FeAnOr on April 23, 2004, 11:40 AM
I did not feel that the soluction presented by myndfyre was very logical in the way I think about my application running, so I would rather do it a different way.


Maybe you could tell us what classes you're talking about here, because multiple inheritance seems to be a very specialized requirement.  If you told us what you were doing, we might be able to come up with some suggestions.
Title: Re:[C#] Inheriting from multiple classes
Post by: MyndFyre on April 24, 2004, 12:23 AM
Quote from: DaRk-FeAnOr on April 23, 2004, 11:40 AM
I did not feel that the soluction presented by myndfyre was very logical in the way I think about my application running, so I would rather do it a different way.

That is without a doubt the only possible way to make a class inherit multiple classes in C#.  Perhaps you didn't understand my specification.

You can't say:

Class1 IS_A Class2 AND Class3 AND Class4

However, you can say:
Class1 IS_A Class2
Class2 IS_A Class3
Class3 IS_A Class4

That's classical inheritence, and the only way you can use the polymorphic properties of it in C#, such as:


Class4 c4 = new Class1();


You can implement multiple interfaces in this way, though.  For example:

Class1 IS_A IEnumerator AND IEnumerable
allows you to say:

IEnumerator ie = new Class1();
IEnumerable enumer = (IEnumerable)ie;


The alternative is to use Ad-Hoc inheritence.  Essentially:


public class Class1 {
 private Class2 c2;
 private Class3 c3;
 private Class4 c4;

 public Class1() {
  c2 = new Class2();
  c3 = new Class3();
  c4 = new Class4();
}

public void Class2Method() {
 c2.Class2Method();
}
public void Class3Method() {
 c3.Class3Method();
}
public void Class2Method() {
 c4.Class4Method();
}
}

However, that doesn't allow you to say at compile-time, that a Class1 instance is the same as a Class4 instance.  For such, you need to use interfaces or multiple parent classes.