tags:

views:

320

answers:

5

Hi, I'm trying to combine the functions of two different base classes into a new class, to no avail. Say I have class A and B, whereas B is a descendant of A with different functionality (i.e. can't be used as a substitute for A during runtime) and need a class C, which combines A and B, uses both and provides a unique interface to users of C. How can I achieve this? Multiple inheritance isn't possible with C#, interfaces don't seem to fit. So what can I do?

Regards

+13  A: 

Your design is broken.

B is a descendant of A with different functionality (i.e. can't be used as a substitute for A during runtime)

That situation is a mis-use of inheritance. If you want to do that, you should either use composition or a common interface that both A & B implement separately instead.

Joel Coehoorn
+1 for composition
Luhmann
Hmm. Thanks for the flowers. A and B are classes from an external type library, namely SecureBlackbox, especially classes TElSecureServer and TElDTLSServer. The latter inherits from the first. Whereas the first may be used to secure TCP connections, the latter is used for UDP. I have to support both in my app, and would be done fine, if I could roll my own class, a descendant of TElDTLSServer. Unfortunatley if I inherit from TElDTLSServer only, I cannot use my class to secure TCP anymore. It does simply not work and end up in an explicit error (for some reasons, I don't know).
neil
Composition does not work, because I need to associate external objects with the used base classes. The base class is able to reflect a reference to itself in some callbacks (need it). If I use combination, the reference points to the pure base class object and I'm loosing the context. So I have to inherit a new class from the base class, add my objects, in order to retrieve the context... Oh what a mess....
neil
Joel Coehoorn
I have uploaded some explanations here http://maps.alphadex.de/eldos/explained.txt
neil
A: 

You say that B descends from A but is different and cannot be used as an A; this defies the purpose of inheritance altogether. What are you trying to achieve? Regardless, it sounds as though you should be using composition, not inheritance. If C does not expose the interface defined by A or B, then it should not inherit from either.

Will Vousden
Its not me, providing A and B. External typ lib. I was in the hope, I could use B and return it to A function with a setting, but this isnt the case. B does not work for TCP connections (see above).
neil
+1  A: 

Consider explicit interface implementation:

http://msdn.microsoft.com/en-us/library/ms173157.aspx

public interface IFriendly
{
  void Greet();
}

public interface IEnemy
{
  void Greet();
}

public class SomeGuy : IFriendly, IEnemy
{
  //default Greeting
  public void Greet()
  {
    //...
  }

  //greeting when using an IFriendly reference
  void IFriendly.Greet()
  {
    //,,
  }

  //greeting when using an IEnemy reference
  void IEnemy.Greet()
  {
    //,,
  }
}
David B
+0.5 ... the other 0.5 you'll get when you remove the `public` keyword from the explicit interface method implementation signatures :->
herzmeister der welten
A: 

The fact that you need to consider such an action makes me think that you might need to consider refactoring your class structure.

But, to solve your immediate problem why not instantiate the two classes (ignore the fact that they are in an inheritance relationship) and call the methods in the sequence you need? Think of C as a facade pattern for A and B.

Paul Sasik
Two classes would help, but because the interfacing is nearly identical, I would have to branch too much (in other words: I have to care about, what type of underlaying object I'm facing, whereas I don't want to care at all)
neil
For those of you demanding concrete information: I have put some detailed information here: http://maps.alphadex.de/eldos/explained.txtPointers welcome.
neil
A: 

Problem solved. Using System.Reflection now.

neil