0

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)
    {

    }
}
4
  • 2
    The usual way to do this is to use composition instead of inheritance. (E.g. ClassThatIsBothClass1andClass2 would inherit from Class1 and contain Class2.) Commented Feb 2, 2013 at 21:06
  • And use interfaces. E.g. have IBoth : IFirst, ISecond. And then have a class Both which implements IBoth and has in it two objects of type First and Second to which it forwards method calls and/or property values. Commented Feb 2, 2013 at 21:11
  • (Thanks for the responses everyone) i'm learning and trying to do this proper. svick, See my thought on composition below and please respond, Im just trying to wrap my head around this for later. and Alex, the thing with that is I wanted to easily be able to combine these things in various places and I have more then two so I would end up making a ton of interface wrappers for Every combination of the classes. Commented Feb 2, 2013 at 21:27
  • But you don't have to write a ton of ifc wrappers, you can (and should) only reference the object for a specific purpose, e.g. you should write your methods so they reference the least common denominator, e.g. DoSomethingToIFirst(IFirst first). Then if you have a selectable, it implements ISelectable (amongst other things) and you can and should reference it in other places only as such. If you code in this way you rarely need ISelectableFocusableMovableImage. Commented Feb 2, 2013 at 21:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Conceptually that is a bit weird. I was planning on calling the classes things like Draggable,Selectable,Ownable, Image, Button. So I can mix and match my final screen objects when I need to. If I create a class that has an instance of draggable, Thats kinda weird.
I'm not entirely sure what you are trying to achieve (I have only seen a very small fragment of your code) If it helps, I tend to think along the lines of adding behaviours to classes (especially UI classes in winforms) to make them act differently. You behaviour can be encapsulated in a class and added to the class you want to behave differently. This is just a form of composition, and it means that you don't have to write a whole bunch of wrappers for classes in the manner you propose.
2

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.

5 Comments

Thanks for the response. 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?
I would probably distill out the set of event handlers and state changers that are absolutely common to every case (Button, Image, FlyingSquirrell) and put those in a completely generic AshtonControl base class. All of the other controls would derive from that common base class. Then I would layer the other concepts on top of that to account for differences in the more specific controls. You could still use interfaces and composition, for example, to create a Button, an Image and an ImageButton.
I would be careful about too much inheritance. Having 7 layers of inheritance is terrible. A much nicer solution is to have an 'ISelectController', which would have a method 'ShouldSelect(ISelectable selectable, IHost host)' and determine if it should select. Then you can keep an instance of it in all 'ISelectable' controls. Notice how the key is to have clear contracts - interfaces. A control can implement many, and should only be referenced by client code for the purpose it fulfills and by the most narrow ifc for this purpose.
I think I was only talking about 3 levels, with a little composition thrown in... Control -> Button -> ImageButton(contains Image). :-) But I totally agree with your comment, the one caveat being that if an image and a button need to do different things in response to a click event, then base class implemenations might be useful. On the other hand, it may well be that an ImageButton needs to behave in its very own distinctive way to a click, not exactly the same as an image or a button alone, and a base class might REALLY get in the way then...
...if the behavior needs to be unique for an ImageButton click, versus a regular Button or Image click, then interfaces seem very clean. Just define your event handlers in your interface(s) and use a worker class like @h.alex recommended for the parts that truly are generic. A deep class hierarchy sort of fights against the concept of separation of concerns. At some point, it's hard to really keep all the behavior and side effects of your ancestor classes in mind, or in check for that matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.