views:

82

answers:

3

Let's say, I can not modify class A.

I have this in class A<T>: There is a JumpNext() method:

public void JumpNext();

It fires an event in class A<T>, called Next

And this in my class B:

public T Next()
{
    instanceofA.JumpNext();
    //instanceofA.Next += something;
    // wait for event
    // someting(object sender, AEventArgs e)
    //return e.Entry
}

The problem is, that my Next method has to return the instance of T contained in the Entry filed of AEventArgs returned by the A.Next event.

So How can I do this inside one method in class B?

I'm using .net 2.0, but if it is indeed possible in any later version only, that might be fine also.

Thx

+2  A: 

You could do something like that :

    public void T Next()
    {
        T value = default(T);
        EventHandler<AEventArgs> handler = new EventHandler<AEventArgs>(delegate(object sender, AEventArgs e) { value = e.Entry; });
        instanceofA.Next += handler;
        instanceofA.JumpNext();
        instanceofA.Next -= handler;
        return value;
    }
Thomas Levesque
edited to use the 2.0 syntax the OP requested.
Jonathan
Beat me by one minute. I would note that the lamba function syntax isn't a feature of C#2.0, so Thomas Levesque's solution would require you use a newer version of the framework. A good discussion of the difference is at http://blogs.msdn.com/ericlippert/archive/2007/01/10/lambda-expressions-vs-anonymous-methods-part-one.aspx.Also note you need to handle the possibility that JumpNext() throws, hence the finally block in my solution.
anelson
@Jonathan : Thanks for the edit ;) - @anelson : yes, it would be better to use a try/finally block, I only omitted it for the sake of simplicity...
Thomas Levesque
This still uses a lambda expression, which is C# 3.0. This wouldn't work in C# 2.0: delegate (sender, e) => { value = e.Entry; }. It should be: delegate (sender, e) { value = e.Entry; }
jrista
Yes... rolling back to my previous version ;)
Thomas Levesque
+3  A: 

If I understand your question correctly, you want to handle the A.Next event inside the B.Next() method. You can do that with an anonymous method, but you must be careful not to register the event handler more than once:

Say the Next event is defined as a delegate declared thusly:

public delegate vote NextHandler(object sender, AEventArgs e);

you could do:

public T Next()
{
    T entry;
    NextHandler handler = delegate(object sender, AEventArgs e) {
        entry = e.entry;
    }

    instanceOfA.Next += handler;
    try {
        instanceOfA.JumpNext();
    } finally {
        instanceOfA -= handler;
    }

    return entry;
}
anelson
Good call on using the finally clause in case JumpNext throws an exception.
Jonathan
Edited to fix compile error in delegate.
anelson
A: 

I did the combination of the two, it seems to be ok now, thx again.

  public T Next()
    {
        T entry = default(T);
        EventHandler<AEventArgs> handler = delegate(object sender, AEventArgs e)
        { entry = e.Entry; };
        instanceofA.Next+= handler;
        try
        {
            instanceofA.JumpNext();
        }
        finally
        {
            instanceofA.Next-= handler;
        }
        return entry;
    }
Newszi