Half a step away from Multiple Inheritance (a suggestion)

A

Andrew Ducker

Let's say you have three classes, ClassA, ClassB and ClassC. You'd
like ClassC to descend from ClassA and ClassB (because, for instance,
ClassA is a UserControl and Class C is all of your validation code).
At the moment you can't do it - your code either has to be in a static
helper class or copied into each class manually.

What if, as a way of checking that your code fulfilled an interface,
rather than checking that all the required methods were in place, it
could optionally see if your class could be cast to an implementation
of that interface. Like so:

class ClassC : ClassA , IClassB
{
private ClassB classB = new ClassB();
public static implicit operator IClassB(ClassC classC)
{
return classC.classB;
}
}

(where ClassB implements IClassB).

So that when calls to ClassA methods were needed they'd be passed
straight back up to it. And when calls to ClassB methods were needed it
would do a cast to ClassB and call them that way.

Have I just reinvented something really obvious? Or is there a mistake
in my thinking? Or something else?

Cheers,

Andy
 
J

Joanna Carter [TeamB]

"Andrew Ducker" <[email protected]> a écrit dans le message de (e-mail address removed)...

| Have I just reinvented something really obvious? Or is there a mistake
| in my thinking? Or something else?

This kind of thing has been a part of Delphi for a long time.

type
TThing = class(TInterfacedObject, IOther)
private
fOther: IOther;
property Other: IOther read fOther implements IOther;
...
end;

Very useful and sorely missed in C#.

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top