views:

76

answers:

2

How do I implicitly convert a delegate to other?

// old
public delegate OldClickEventHandler(object sender, OldClickEventArgs e);
class OldClickEventArgs
{
 public int intEnumValue;
 OldClickEventArgs(int enumValue){ this.intEnumValue = enumValue; }
}

// new
public delegate NewClickEventHandler(object sender, NewClickEventArgs e);
class NewClickEventArgs
{
 public MyEnum EnumValue;
 NewClickEventArgs(MyEnum enumValue){ this.EnumValue = enumValue;  }

 public static implicit operator NewClickEventArgs(OldClickEventArgs e) {
  return new NewClickEventArgs((MyEnum)e.intEnumValue);
 }
}

// class NewButton : OldButton
// here I need to implicitly convert EventHandlers. HOW?
//
public event NewClickEventHandler Click
{
    add {
        oldObject.Click +=  value; // cannot convert New.. to Old..
    }
    remove {
        oldObject.Click -=  value; // cannot convert New.. to Old..
    }
 }
+3  A: 

In order to assign the eventhandler they have to have the same signature. In you case you can achieve this if your NewEventArgs extends OldEventArgs. E.g.

class OldEventArgs : EventArgs
{
  // ... implementation ..
}

class NewEventArgs : OldEventArgs
{
  // ... implementation ...
}

After this you should be able to assign the NewEventHandler to the old event.

Obalix
I inherited `NewEventArgs` from `OldEventArgs`. However, the compiler tells me that can't convert `OldEventHandler` to `NewEventHandler`.
serhio
+1  A: 

I guess you can't unless you want NewEventArgs to inherit from OldEventArgs, but perhaps this approach can do the trick

edit: the previous classes were a mess (and didn't work). These should :-)

private Converter c = new Converter();

// when you want to trigger oldevent, call c.fireOld(sender, args);

public event OldEventHandler OldEvent {
    add { c.oldH += value; }
    remove { c.oldH -= value; }
}

public event NewEventHandler New {
    add { c.newH += value; }
    remove { c.newH -= value; }
}

public class Converter {
    public event OldEventHandler oldH;
    public event NewEventHandler newH;

    // call both old and new
    public void fireOld(object o, OldEventArgs args) {
        oldH(o, args);
        newH(o, args);
    }
}
Patrick
how do you initialize 'c'?
serhio
I can't modify OldEventArgs, cause inherit from a third party AcX wrapper.
serhio
Edited the post, you should be able to use them now. Just initialize c wherever, in constructor or directly. Whenever you call "Old()", new will also trigger
Patrick
Thanks, Patrick. Well, I don't call Old. Just I have `NewButton : OldButton`, and redirect the events from `OldButton` with the neweventhandler. So, I have no let's say `OldClick` or `NewClick`. I have the same (modernized with NewEventHandler/Args) `Click`. I don't understand how do you associate your converter with the object itself.
serhio
When you want to receive events of old, you do obj.OldEvent += myMethod. The only differance is that the newevents also get triggered when you fire the OldEvent. I was under the impression that you somehow wanted to combine OldEvent with NewEvent?
Patrick