473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Delegates/events

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<EventArgsMyEvent;
}
//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<EventArgscallback)
{
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
Mar 23 '07 #1
7 1213
On Mar 23, 3:37 pm, "Peter Larsen []" <PeterLar...@newsgroup.nospam>
wrote:
I have a problem passing an event in a method call.
You can't pass an event. An event is basically a pair of methods to
add/remove handlers. The encapsulation prevents you (unless another
means is provided) of seeing what the current handlers are.

See http://pobox.com/~skeet/csharp/events.html for more on this.

Jon

Mar 23 '07 #2
PS

"Peter Larsen []" <Pe*********@newsgroup.nospamwrote in message
news:u$**************@TK2MSFTNGP03.phx.gbl...
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<EventArgsMyEvent;
}
//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<EventArgscallback)
{
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<EventArgsMyEvent;
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<EventArgscallback)
{
if(callback != null)
callback(this, new EventArgs());
}
}

PS
Mar 23 '07 #3
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<EventArgsMyEvent;
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<EventArgsEH = new
EventHandler<EventArgs>(aInstance.OnMyEvent);
th.AMethod(EH);
}
}

public class Third
{
public void AMethod(EventHandler<EventArgscallback)
{
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Mar 26 '07 #4
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Mar 28 '07 #5
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<EventArgsMyEvent;
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
Mar 28 '07 #6
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Mar 29 '07 #7
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
Mar 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.