Hi,
Actually I m little new to programming...i m learning c# and can'nt figure out wat is EVENT...
I saw calling events in the middle of code like....
ws.ProgressChanged += new ProgressChangedEventHandler(Upload_ProgressChanged);
I think this statement invokes function "Upload_ProgressChanged"....
but can't we write it simply like
Upload_ProgressChanged(); ...I mean like calling a function...
plz explaing me sumthing regarding events and delegates.....
tnx...
George MarianPosted Mar 17, 2007, 12:57 AM
This article is an introduction to delegates, but it doesn't go into events.
http://msdn.microsoft.com/msdnmag/issues/01/04/net/default.aspx
In a nutshell, delegates are objects that point to methods, and can be used to call those methods.
That line of code doesn't call the function, it passes a delegate that warps the method which will be executed when the event occurs.
If you need to call that function, you call it as you normally would. However, a control on a form wouldn't know to call that method. That's where the event delegate comes in. It provides a way for you to pass a reference to method that needs to be called when a certain event occurs.
The trick is that the event delegate and the method it warps will have the same signature. That is the return type and the parameter types will be of the same type for both. So, the control will know how to call any appropriate method, because it will know the signature of the event delegate. When an event occurs, it calls any event delegate that has been registered to handle that event. Note, this can be more than one method. That's why that line of code uses the += operator, instead of the = operator.
Edit: I forgot to mention that this is only one way to use delegates.
I saw an interesting example in a book. Basically, they created a class to execute math operations on two values. However, that class didn't implement those operations. They were implemented in methods outside of the class. These methods were passed to an instance of the class using a delegate. That instance would then perform the appropriate operation on the values by calling the delegate. The instance didn't need to know anything about the operation it was peforming, it only needed to know how to call the delegate.
Basu BiswasPosted Mar 16, 2007, 11:19 PM
tnx for the reply....
I got wat is event...but I don'nt know where to use these evens and delegates...
As i develop some windows application like uploading files to the server...
in such application where i can use thouse....
Whatever sample program i found on the net it says...
This program is not intended to be used for any useful purpose...
is it that events are used only while creating some controls....
if possible...do reply...
tnx a lot again....
Ripal SoniPosted Mar 16, 2007, 2:46 PM
when you have any control in your programme for example you have button in your form and you want some code to run when you click on that button ,so that is the click even of that button ,if you want some code to run whe you double click on that button then you have to create its its double_click event. i don't have a perfect defination but event is just action of the control ,forexample when you want something when the form is loading then you will need form_Load even ,generally such events are created by itself on doubl clicking on that control
if you want load() even of form then in your design view when you double click on that form it will create its form_load event by it self and take you to the code view inside that event.
the example you wrote in this is the example of creating an event ,if you double click the control of the form then it will only create its load even in code view but if you want forms unload even then you can do 2 things u can either go to the propery of the form in design view then from its even property if doubleclick on its unload even then it will create that for you or you can write its custom code just like that example code you wrote in your question
i hope from this you get little idea about events.you can not write function instead of even
because event is something you want to run when you do some action on your control of the form ,function you can call that function inside that event but instead of event you can not write function