Valhalla Legends Forums Archive | .NET Platform | [C#] Inheriting from multiple classes

AuthorMessageTime
DaRk-FeAnOr
I am trying to have one class inherit the methods of many classes. The code that I tried was:
[code]
public class Test : Test1, Test2, Test3, Test4{
[/code]
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
[/quote]
Thank you for any help that you can offer.
April 15, 2004, 11:46 PM
Myndfyr
[quote author=DaRk-FeAnOr link=board=37;threadid=6319;start=0#msg55340 date=1082072772]
I am trying to have one class inherit the methods of many classes. The code that I tried was:
[code]
public class Test : Test1, Test2, Test3, Test4{
[/code]
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
[/quote]
Thank you for any help that you can offer.
[/quote]

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).
April 15, 2004, 11:52 PM
DaRk-FeAnOr
I see, I will figure out a way around this then. Thanks.
April 15, 2004, 11:55 PM
Tuberload
[quote author=DaRk-FeAnOr link=board=37;threadid=6319;start=0#msg55344 date=1082073322]
I see, I will figure out a way around this then. Thanks.
[/quote]

Myndfyre just told you the way around the problem...
April 16, 2004, 12:15 AM
Myndfyr
[quote author=Tuberload link=board=37;threadid=6319;start=0#msg55352 date=1082074525]
[quote author=DaRk-FeAnOr link=board=37;threadid=6319;start=0#msg55344 date=1082073322]
I see, I will figure out a way around this then. Thanks.
[/quote]

Myndfyre just told you the way around the problem...
[/quote]

The obvious escapes all but the most brilliant... ;)
April 16, 2004, 1:00 AM
Tuberload
[quote author=Myndfyre link=board=37;threadid=6319;start=0#msg55359 date=1082077215]The obvious escapes all but the most brilliant... ;)
[/quote]

Sorry for the pointless post, but that was good. ;D I love a good laugh.
April 16, 2004, 1:28 AM
DaRk-FeAnOr
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.
April 23, 2004, 4:40 PM
K
[quote author=DaRk-FeAnOr link=board=37;threadid=6319;start=0#msg56462 date=1082738450]
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.
[/quote]


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.
April 23, 2004, 6:34 PM
Myndfyr
[quote author=DaRk-FeAnOr link=board=37;threadid=6319;start=0#msg56462 date=1082738450]
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.
[/quote]

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:

[code]
Class4 c4 = new Class1();
[/code]

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

Class1 IS_A IEnumerator AND IEnumerable
allows you to say:
[code]
IEnumerator ie = new Class1();
IEnumerable enumer = (IEnumerable)ie;
[/code]

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

[code]
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();
}
}
[/code]
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.
April 24, 2004, 5:23 AM

Search