Reputation:
Let's say, I can not modify class A.
I have this in a 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
field 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.
Upvotes: 0
Views: 1057
Reputation: 41
I did the combination of the two:
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;
}
Upvotes: 0
Reputation: 292375
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;
}
Upvotes: 2
Reputation: 2599
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;
}
Upvotes: 3