• Welcome to Valhalla Legends Archive.
 

[C#] Inheriting from multiple classes

Started by Dark-Feanor, April 15, 2004, 06:46 PM

Previous topic - Next topic

Dark-Feanor

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.
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

MyndFyre

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).
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Dark-Feanor

I see, I will figure out a way around this then. Thanks.
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

Tuberload

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...
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

MyndFyre

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...  ;)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Tuberload

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.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

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.
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

K

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.

MyndFyre

#8
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.