Ashton
Ashton

Reputation: 1

How To Get around multiple inheritance

I am trying to implement the following in C# so that I can force initialization of certain event delegates and variables in the parent classes, or I would use interface's instead. Obviously the below is not syntax correct.

the concrete class is Class1 & class 2.

the idea being here MyClass Is a button and it is an Image and it is something else.

Edit: " I understand that selectable and others are not objects but states. what I really want to do is to write the code that maintains the selectable state in the appropriate method because it will be the same for all of them. i.e. On click event( click location) check if i was I clicked based on my bounding box, update state to selected. I am in XNA, which is a c# polling environment, and I'm attempting to make the GUI as event driven as possible, if that makes any sense? "

 public abstract class Class1
    {
        private int NumberNeededForMethod;

        private void methodThatOccursWhenEventHappens(int NumberNeededForMethod)
        {
            // stuff using NumberNeededForMethod;
        }

        private Class1(int NumberNeededForMethod)
        {
            MethodDelegate += methodThatOccursWhenEventHappens(int
            NumberNeededForMethod)
            ;
        }
    }


public abstract class Class2
    {
        private int NumberNeededForMethod2;

        private void methodThatOccursWhenEventHappens2(int NumberNeededForMethod2)
        {
            // stuff using NumberNeededForMethod2;
        }

        Class2(int NumberNeededForMethod2)
        {
            MethodDelegate += methodThatOccursWhenEventHappens(int NumberNeededForMethod2);
        }
    }

public class ClassThatIsBothClass1andClass2: Class1, Class2
{
    ClassThatIsBothClass1andClass2( int NumberNeededForMethod1, int NumberNeededForMethod2) : Class1(NumberNeededForMethod1),Class2(NumberNeededForMethod2)
    {

    }
}

Upvotes: 0

Views: 1017

Answers (2)

Craig Tullis
Craig Tullis

Reputation: 10507

First, of course, C# does not support multiple inheritance, so whatever polymorphism you implement will have to be accomplished using interfaces and composition.

Referring to this comment: Draggable, Selectable and Ownable are attributes of an object.

Image and Button, on the other hand, are objects.

A Button cannot be an Ownable.

But a Button can be Ownable, Draggable or Selectable. What I wonder is whether those attributes aren't just properties on a single IAshtonControl interface?

A Button can conceivably also be an Image. That makes perfect sense.

Because C# lacks multiple inheritance, you simply cannot create an AshtonButton class that derives from both Button and Image base classes.

One thing you can do is create an AshtonButton class that implements the IAshtonControl interface, and the implementation for that interface can delegate to a private instance of a worker class that does whatever work is common to all IAshtonControl instances.

Or you could have separate IOwnable, IDraggable and ISelectable interfaces if that is what is required.

Either way, it becomes possible to truthfully make the statement that AshtonButton is an IAshtonControl, is ownable, is draggable, is selectable. Those things might have different meanings (different behavior/visual effects) for different controls, or they might not, but you would hide those implementation details behind the interface(s) so that you could programmatically treat each object the same way regardless of its implementation.

It is important to separate the object from its attributes, because that affects the way you think about the problem. Draggable is not a thing, it is a characteristic of a thing.

But if your goal is to have a Button that is also an Image, some type of composition or delegation is the way to accomplish that. If you have a IAshtonImage interface, then you would implement that on both the AshtonImage class, and on the AshtonImageButton class. Then you have an internal instance (composition) of the AshtonImage class, within the AshtonImageButton class, and delegate calls to the IAshtonImage members through to the private (composed) AshtonImage instance, and so on.

Upvotes: 2

Jay
Jay

Reputation: 10128

You can use composition to create a class which wraps class1 and class2 and is the thing that responds to the event raised by your button.

Upvotes: 2

Related Questions