Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
What is the difference between using Delegates or using AddHandler and RemoveHandler? Are AddHandler and RemoveHandler actually using delegates behind the scenes anyway? If so, why would you use deletages manually?

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

For events that are fired/handled within the same class, I just use Event Handlers, since this seems to me the path of least resistence.

 

Where I have used Delegates is when I want to "catch" events that are fired in another layer of my application.

 

So, I have my "listener" in my UI project, and pass the address of this function into my constructor of my Domain Logic object.

 

Later, something will happen in the Domain Logic that needs to "raise" some UI event, and this is where I just use that addressOf my delegate (that was passed by-reference) and fire off this UI function from within my Domain Logic.

 

When I was deciding how to handle this, I had others just recommend a timer that kept looking at some property on the Domain Logic, but this Delegate function seemed to be a better solution.

Posted

That's not quite what I meant. In your example I agree, delegates provide a much better solution to using a timer. Interrupt or Event driven processes are always better than Polling processes as there is no wasted effort.

 

What I'm asking is why would you go through the effort of using delegates directly when AddHandler and RemoveHandler do all the hard work for you?

 

I think I've discovered one reason. AddHandler and RemoveHandler are VB.NET only so you can use them if your just interested in VB.NET programming but if you want to use C# or are considering using C# then you should get used to using delegates directly.

TT

(*_*)

 

There are 10 types of people in this world;

those that understand binary and those that don't.

Posted

Actually, in C#, instead of addHandler/removeHandler, you use:


controlInstance.SomeEvent += .....
controlInstance.SomeEvent -= .....
[/Code]

but, you get the same functionality.

 

 

Also, in my example, I don't think you can use AddHandler/RemoveHandler in my case - since the UI makes a reference to the Domain Logic project and the Domain Logic is trying to raise a specific event on the UI. It becomes a circular-reference and you won't compile if you try this, unless I missed something.

 

This is the only way I got this process to work.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...