Delegates/events

P

Peter Larsen []

Hi,

I have a problem passing an event in a method call.
Please see the following sample:


//This class holds information about listeners (in MyEvent).
public class First
{
public event EventHandler<EventArgs> MyEvent;
}


//This class could be a worker-thread.
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
th.AMethod(aInstance.MyEvent); //THIS IS where i dont know
what to do !!!!!!!!!
}

}


//This class do some work for the worker-thread and is supposed to notify
the listeners (in MyEvent) about the ongoing work.
public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if (callback != null)
callback(this, new EventArgs());
}
}


My problem is in class Second. How do i pass the event (in th.AMethod()) ??
Thank you in advance.
BR
Peter
 
P

PS

Peter Larsen said:
Hi,

I have a problem passing an event in a method call.
Please see the following sample:


//This class holds information about listeners (in MyEvent).
public class First
{
public event EventHandler<EventArgs> MyEvent;
}


//This class could be a worker-thread.
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
th.AMethod(aInstance.MyEvent); //THIS IS where i dont know
what to do !!!!!!!!!
}

}


//This class do some work for the worker-thread and is supposed to notify
the listeners (in MyEvent) about the ongoing work.
public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if (callback != null)
callback(this, new EventArgs());
}
}


My problem is in class Second. How do i pass the event (in th.AMethod())
??

Is this what you mean?

public class First
{
public event EventHandler<EventArgs> MyEvent;
public void OnMyEvent(object sender, EventArgs args)
{
if(this.MyEvent != null)
this.MyEvent(sender, args);
}
}
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
th.AMethod(aInstance.OnMyEvent);
}
}
public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if(callback != null)
callback(this, new EventArgs());
}
}

PS
 
J

Jeffrey Tan[MSFT]

Hi Peter ,

In "AMethod", the parameter "callback" is of type
"EventHandler<EventArgs>", which is a delegate instead of event. So you can
not pass an event to this method. You should explicitly construct a
"EventHandler<EventArgs>" delegate(which must explictly wrap a method) and
pass this delegate to the method, like this:

public class First
{
public event EventHandler<EventArgs> MyEvent;
public void OnMyEvent(object sender, EventArgs args)
{
if (this.MyEvent != null)
this.MyEvent(sender, args);
}
}
public class Second
{
public void Method(First aInstance)
{
Third th = new Third();
EventHandler<EventArgs> EH = new
EventHandler<EventArgs>(aInstance.OnMyEvent);
th.AMethod(EH);
}
}

public class Third
{
public void AMethod(EventHandler<EventArgs> callback)
{
if (callback != null)
callback(this, new EventArgs());
}
}

Additionally, the code snippet provided by "PS" is almost the same as this,
but it uses an implicit way of constructing "EventHandler<EventArgs>". That
is when passing "aInstance.OnMyEvent" to the method, the C# compiler will
emit an explicit construct to "EventHandler<EventArgs>" delegate like below:
public void Method(Form1.First aInstance)
{
Form1.Third th = new Form1.Third();
th.AMethod(new EventHandler<EventArgs>(aInstance.OnMyEvent));
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Peter ,

Have you reviewed all the replies to you? Do they make sense to you? If you
still need any help or have any concern, please feel free to feedback,
thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Larsen []

Hi,

Sorry for my late reply, but i have been away for some days.

Thank you for your reply's.
I understand that it isn't possible to pass an event like that. And i also
understand the way you have solved the problem.
I have already solved the problem in a similar way, but for me it was a
temporary solution until a better solution was found. But i think it is
going to be a permanent solution now :)

I have chosen to create a carrier class (in this sample, class First has
become this carrier) to carry the event and to fire the notification.
Its very similar to what you did, except that i use class First as parameter
instead of event "EventHandler<EventArgs>" as parameter.

public class First
{
public event EventHandler<EventArgs> MyEvent;
public void FireEvent(object sender, EventArgs args)
{
if (MyEvent != null)
MyEvent(sender, args);
}
}

public class Second
{
public void Method(First eventPlaceHolder)
{
Third th = new Third();
th.AMethod(eventPlaceHolder);
}

}

public class Third
{
public void AMethod(First eventPlaceHolder)
{
if (eventPlaceHolder != null)
eventPlaceHolder.FireEvent(this, new EventArgs());
}
}

BR
Peter
 
P

Peter Larsen []

Hi,

Sorry for my late reply, but i have been away for some days.

Thank you for your reply's.
I understand that it isn't possible to pass an event like that. And i also
understand the way you have solved the problem.
I have already solved the problem in a similar way, but for me it was a
temporary solution until a better solution was found. But i think it is
going to be a permanent solution now :)

I have chosen to create a carrier class (in this sample, class First has
become this carrier) to carry the event and to fire the notification.
Its very similar to what you did, except that i use class First as parameter
instead of event "EventHandler<EventArgs>" as parameter.

public class First
{
public event EventHandler<EventArgs> MyEvent;
public void FireEvent(object sender, EventArgs args)
{
if (MyEvent != null)
MyEvent(sender, args);
}
}

public class Second
{
public void Method(First eventPlaceHolder)
{
Third th = new Third();
th.AMethod(eventPlaceHolder);
}

}

public class Third
{
public void AMethod(First eventPlaceHolder)
{
if (eventPlaceHolder != null)
eventPlaceHolder.FireEvent(this, new EventArgs());
}
}

BR
Peter
 
J

Jeffrey Tan[MSFT]

Hi Peter,

Thank you for sharing the result!

Yes, I agree your current solution is applicable and suitable. Anyway, we
have several ways to achieve the result :)

If you need further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Larsen []

Hi Jeffrey,

Yes i know there must be other ways to solve this problem.
For me it was an important information to learn that it isn't possible to
pass an event as a parameter in a method call - thanks.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top