how delegates help us in our real life applications

seco

Well-known member
Joined
Mar 4, 2007
Messages
66
Programming Experience
Beginner
Hi
i can make delegates good

but what is the time that delegates that come in handy i mean i need example that shows the delegates solves a problem
or question with another way when i use delegates in my applications to simplify work and reduce time

thanks in advance.
 
Delegates are function pointers. For me i use this to put several functions in an array. For example; at runtime, the user have to input something which will change the course of action of the program. The program, then has a choice too, in which method that particular user action will be much more appropriate.
 
Hi
i can make delegates good

but what is the time that delegates that come in handy i mean i need example that shows the delegates solves a problem
or question with another way when i use delegates in my applications to simplify work and reduce time

thanks in advance.

There are several times when you program, that you want your class to be able to announce that something has happened, but you have NO IDEA what class will be listening to that announcement..

For example.. Let's say that, in 2004, Microsoft wrote a class called Button. It draws a button on a form, and when the button is clicked, it makes an announcment (the clicked event)

Microsoft, 3 years ago, couldnt possibly know that in 2007, I would want to write a class called FlobbleRunner, that listens to that button click, and does something.. So in 2004, they couldnt write code for the button that goes like:

'when user clicks, run cjard's FlobbleRunner method
Dim x as New FlobbleRunner
x.RunFlobble()


MS jsut CANT do that.. So they write a delegate instead.. A delegate is an object that points to a function or sub. Every time you call .Invoke() on the delegate, the .NET framework runs the code in whatever function/sub the delegate is attached to..

Thus, I can take that delegate from Button, and clip it onto my method. Button doesnt know or care what my method is called, where it is, what is does.. it just runs it. Its my job to do the rest.

For another example and a deeper discussion on delegates, search the forum for "tutorial: delegates"
 
As for how they reduce work and save time.. In one of my apps, I have a SearchForm that displays the results of a search. Doubleclicking a line in the grid opens up that result in the relevant form that handles it.. be it the addresses screen, the invoices screen, the client screen.. whatever. Every screen in the app is searchable.

All the searchform does is have a delegate that is poked (and supplied with an argument as to what the user picked) when the user picks something

Now any form in my app can have a search.. The form prepares a query and hands it to the searchform. The form also registers a delegate association to a local method. The searchform runs the query and shows the results. It then delegates the picking of the result

As such, in my client form i can have a method called "ShowClient(searchPick as DataRow)", and in my address form I can have "ShowAddress(searchPick as DataRow)" - search form calls either of these depending on what was registered.

Without this, I would have to have all forms in my app descend from ASearchForm parent, which has a method "SearchResultChosen" and the searchform would have to know about the SearchableForm that owns it:

Dim s as New SearchForm(query, Me) 'pass reference to this form into the searchform
s.DoSearch()
...

and then in the searchform:

theSearchableForm.SearchResultPicked(grid.CurrentRow)


..
So yeah ,you say, that will work.. But what if I want my form to inherit from something else too? Why should all my forms have to inherit from searchableform just to be searchable.. They could implement an interface, but again, youre then forced to what to call the method. .What if a form is searchable in 2 ways, or has 2 searches? Then you run into a naming clash because you cant have 2 methods called the same name..

Think about how many click event handlers your average form has; lots! It would be a real headache if every button called a handler on a form that implemented the IClickHandler interface.. As it is, delegates allow us to call the methods nicer names:

OkButton_Click
CancelButton_Click

rather than:

VB.NET:
ButtonCLickHandler(b as Button)
  if b Is OkButton Then
    'do OK routine
  else if b is CancelButton Then
    'do cancel routine..
 
really this is awesome reply i will read it carefully and if something not clear i will back:)

thanks.
 
Back
Top