casting and method over-loading RRS feed

  • Question

  • I am bubbling events in my application and so therefore using the bubble events method.  As this method handles all sorts of bubbled events their is a switch or if statement within it to determine what sort of event we're dealing with.  I was wondering if I could get around this by creating different versions of the event args class.  So let me explain, say I have two types of event that are handled differently called X and Y, I create new event args classes for these two events as they store different types of info.

    public class EventsArgsX : EventsArgs

    public class EventsArgsY : EventsArgs

    then when I RaiseBubbleEvent from somewhere in my application I can pass either of the two event arg based types, so..

    EventArgsX foox = new EventArgsX();


    EventArgsY fooy = new EventArgsY();

    then the OnBubbleEvent method picks this up, who's signature is
    override OnBubbleEvent(object source, EventArgs e)

    now i cant overload this method as its overriden in the first place, so what I thought I could do was have another method with overloads in it to handle this, so

    protected override OnBubbleEvent(object source, EventArgs e)

    private void DoStuff(EventArgsY args)

    private void DoStuff(EventArgsX args)

    but of course the problem is that EventArgs e in the OnBubbleEvent method is of type EventArgs at the time of calling.  However we know its not.  So how would i case it back to its actual type in order for the method call to work?

    Many thanks, hope you can help me with this, its seems really easy like a might be missing something or that it just cant be done

    any ideas??


    Friday, January 22, 2010 11:29 AM


  • You could do something like

                if (e.GetType() == typeof(EventsArgsX)
    • Proposed as answer by Geert van Horrik Saturday, January 23, 2010 9:07 AM
    • Marked as answer by Tim Li Monday, January 25, 2010 5:34 AM
    Friday, January 22, 2010 11:35 AM