Click here to Skip to main content
15,923,083 members
Home / Discussions / C#
   

C#

 
GeneralRe: Insertions sort reversed - optimize Pin
bolikej3-Jun-10 2:31
bolikej3-Jun-10 2:31 
QuestionBest performance LAN communication for a Server/Client application Pin
teknolog1232-Jun-10 22:58
teknolog1232-Jun-10 22:58 
AnswerRe: Best performance LAN communication for a Server/Client application Pin
#realJSOP3-Jun-10 1:50
professional#realJSOP3-Jun-10 1:50 
QuestionCreating Events Pin
Roger Wright2-Jun-10 22:37
professionalRoger Wright2-Jun-10 22:37 
AnswerRe: Creating Events Pin
Abhinav S2-Jun-10 23:05
Abhinav S2-Jun-10 23:05 
AnswerRe: Creating Events Pin
OriginalGriff3-Jun-10 0:45
mveOriginalGriff3-Jun-10 0:45 
GeneralRe: Creating Events Pin
Roger Wright3-Jun-10 4:08
professionalRoger Wright3-Jun-10 4:08 
AnswerRe: Creating Events Pin
DaveyM693-Jun-10 1:38
professionalDaveyM693-Jun-10 1:38 
>Hi Roger,

I don't know if you came across my article[^] or not - but it works through from the most basic to more complex.

A quick overview...

Events (using the standard .NET model) use a delegate System.EventHandler which has the two familiar parameters object sender, EventArgs e. The sender is the instance that is raising the event, e is where any information is passed (more on this soon).

So, to create an event you need something like:
public event EventHandler MyEvent;


To raise the event from your class instance is a little more complicated.
protected virtual void OnMyEvent(EventArgs e)
{
    EventHandler eh = MyEvent;
    if(eh != null)
        eh(this, e);
}
The above method is protected virtual so it only visible within the class or derived classes and can be overriden if required. The e parameter lets the calling code pass the info to the event.
The first line in the message body is there to protect against the possibility that MyEvent may become null between checking for null and raising the event - see next line!
If there are no subscribers, then eh will be null so raising the event would cause an exception so the second line checks for this.
The final line actually raises the event.

That's all there is to it until you need to actually pass some information. EventArgs doesn't have anywhere to put these, so you need to create your own class derived from it e.g.
public class MyEventArgs : EventArgs
{
    private int value;

    public MyEventArgs(int value)
    {
        this.value = value;
    }

    public int Value
    {
        get { return value; }
    }
}

To use these, you can change all the EventHandler references above to EventHandler<MyEventArgs> and EventArgs to MyEventArgs.

Now, all you need to do is call the OnMyEvent method when needed and your event will be raised.
Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

GeneralRe: Creating Events Pin
Roger Wright3-Jun-10 4:09
professionalRoger Wright3-Jun-10 4:09 
QuestionSystem.DBNull returned for whitespace strings Pin
GDavy2-Jun-10 22:07
GDavy2-Jun-10 22:07 
AnswerRe: System.DBNull returned for whitespace strings Pin
Pete O'Hanlon2-Jun-10 23:30
mvePete O'Hanlon2-Jun-10 23:30 
GeneralRe: System.DBNull returned for whitespace strings Pin
GDavy2-Jun-10 23:59
GDavy2-Jun-10 23:59 
GeneralRe: System.DBNull returned for whitespace strings Pin
Pete O'Hanlon3-Jun-10 0:23
mvePete O'Hanlon3-Jun-10 0:23 
QuestionProblem reading multiple bytes from serialport Pin
myreki2-Jun-10 21:35
myreki2-Jun-10 21:35 
AnswerRe: Problem reading multiple bytes from serialport Pin
Luc Pattyn3-Jun-10 2:14
sitebuilderLuc Pattyn3-Jun-10 2:14 
QuestionNotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 21:20
professionalMycroft Holmes2-Jun-10 21:20 
AnswerRe: NotifyPropertyChanged and ReadValue() not working Pin
Henry Minute2-Jun-10 22:14
Henry Minute2-Jun-10 22:14 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 22:39
professionalMycroft Holmes2-Jun-10 22:39 
AnswerRe: NotifyPropertyChanged and ReadValue() not working - kludged Pin
Mycroft Holmes2-Jun-10 22:21
professionalMycroft Holmes2-Jun-10 22:21 
AnswerRe: NotifyPropertyChanged and ReadValue() not working Pin
GDavy2-Jun-10 22:25
GDavy2-Jun-10 22:25 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 22:37
professionalMycroft Holmes2-Jun-10 22:37 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
GDavy2-Jun-10 22:53
GDavy2-Jun-10 22:53 
GeneralRe: NotifyPropertyChanged and ReadValue() not working Pin
Mycroft Holmes2-Jun-10 23:27
professionalMycroft Holmes2-Jun-10 23:27 
QuestionA Free Licensing System? Pin
Matt U.2-Jun-10 12:26
Matt U.2-Jun-10 12:26 
AnswerRe: A Free Licensing System? Pin
Richard Andrew x642-Jun-10 17:53
professionalRichard Andrew x642-Jun-10 17:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.