Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have seen so many articles on creating and handling custom events in c#.But in every case I'm seeing they are calling a delegate and the main thing which is to be executed is written in a function.That function is called by the event.

But my question is why should I use an event?I can call the function directly where needed.

So can you provide an article where I can see the actual and practical purpose of creating event.I need discussion in this way-------

1)this is the work of event and
2)this thing can not be achieved by calling function or if we call a function this is the constraints.
Posted
Comments
Stefan_Lang 24-Jun-15 1:51am    
I am not familar with C# and can therefore not offer language-specific advice.

However, events are a language-independend concept: they are most often used to synchronize threads, by notifying them of a change that may be relevant to them. E. g. in Windows, the window drawing routines are within a separate that gets notification events every time something happens that may require a redraw, e. g. a mouse move, mouse click, border drag, a window maximize/minimize/restore command, activating a window, etc..

Users can define events for similar purposes, e. g. if you have a window showing financial data, and another thread streaming current data, the latter can send events every time one of the items that you have flagged for tracking changes. This way you can program a ticker with real time financial data.

You use an Event for example when you want to create a Notification to a someone you don't know while developing the function.

Perhaps see a Button. This Control creates/fires several Events which COULD be used but NOT MUST be used. The Developer of the Button didn't know if someone will use something of this - but he gave the possibility ...
 
Share this answer
 
Comments
souvikcode 24-Jun-15 6:21am    
But actually I can call button events dynamically.I want to know why custom event needed.Where required I can call the function directly.
Can you give an example where event is must used?
souvikcode 24-Jun-15 6:25am    
Your button example is fine.I got it.But I can not raise button click event my self by a function.I can write a function when user click a button.Microsoft has created each and every possible user interaction event for a button.So why I need more?Your point is right.A developer can use event in his way.But he can also use my created function in his way.
Ralf Meier 24-Jun-15 7:00am    
I think, my example is not so bad.
The Problem in the Moment is, that I don't imagine what you want to do.
But we could stay by this example.
It makes no difference, which kind of class you create. Perhaps you create a Button. This Button should work for all users. Now ... which functions would you build in ? Perhaps you only need the Click-Event. But another user could need the Down-Event. Now you do the following : you implement all Events (Actions which come out of your class and for which you don't want to ask), you could imagine that they could be necessary and let the user decide which and how to use them.

Perhaps this is better ...
souvikcode 29-Jun-15 0:52am    
What I have understood is-------------------------

Let I've created a custom event,say grid update event.Now when any cell of a datagridview is changed my event is fired.Right?

Now I have a function which will be executed in this event.

Now for every cell update, my update function will be called automatically,I don't have to call the update function manually.Or,for another developer,he need not to know about the update function.

But,if I call the function manually all time by not creating event,that will also create the perfect development and perfect result,but that's not a good practice and time consuming.

Am I in right way?
Ralf Meier 6-Jul-15 2:25am    
Sorry for the late response to your reply ...
I think, you are in the right way.
Now, what is interestinh in Events is :
Depending on your development, the user gets Information about the Sender of the Event.
Depending on your Eventargs-Class (perhaps you build a customized one) the user gets Information about the actual state of the control when calling the Event - that might be a difference to later calls of an internal method.
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] - it uses an event to pass data between two forms without breaching OOPs rules.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900