Hi Guys
NP106 if (Changed != null)
I got the following program from a book.
Anyone knows please explain the reason. What is the significant of if (Changed != null)?.
Please explain.
Thank you
using System;
public delegate void ChangedEventHandler(object sender, EventArgs e);
public class Student
{
private int idNum;
private double gpa;
public event ChangedEventHandler Changed;
public int GetId()
{
return idNum;
}
public double GetGpa()
{
return gpa;
}
public void SetId(int num)
{
idNum = num;
OnChanged(EventArgs.Empty);
}
public void SetGpa(double avg)
{
gpa = avg;
OnChanged(EventArgs.Empty);
}
public void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e); //invoking the event
}
}
class EventListener
{
private Student stu;
public EventListener(Student student) //constructor of the EventListener class
{
stu = student;
stu.Changed += new ChangedEventHandler(StudentChanged);
}
public void StudentChanged(object sender, EventArgs e)
{
Console.WriteLine("The student has changed.");
Console.WriteLine(" ID# {0} GPA {1}\n", stu.GetId(), stu.GetGpa());
}
}
class DemoStudentEvent
{
public static void
{
Student oneStu = new Student();
EventListener listener = new EventListener(oneStu);
oneStu.SetId(2345);
oneStu.SetId(4567);
oneStu.SetGpa(3.2);
}
}
/*
The student has changed.
ID# 2345 GPA 0
The student has changed.
ID# 4567 GPA 0
The student has changed.
ID# 4567 GPA 3.2
*/
Fernando SotoPosted Jul 2, 2008, 9:56 PM
Posted Jul 2, 2008, 9:21 PM
Thank you for your explanation, Fernando Soto
Fernando SotoPosted Jul 2, 2008, 8:45 PM
To your statement, "Therefore without if (Changed != null) program will execute. ", Yes it will execute BUT if someone uses your code that you write and you do not check to see if anyone is registered to receive the event befor calling the delegate function it WILL FAIL with a null reference excepthion if no one is registered.
Posted Jul 2, 2008, 7:23 PM
That means if (Changed != null) is there to merely check EventListener listener = new EventListener(oneStu) is not null. Therefore without if (Changed != null) program will execute.
Fernando SotoPosted Jul 2, 2008, 6:08 PM
public void OnChanged(EventArgs e)
{
Changed(this, e); //invoking the event
}
and in the main program you removed this line of code
EventListener listener = new EventListener(oneStu);
You will find that the OnChanged method will throw an exception of null reference because removing the listener object from the main code, meaning you do not want to receive Changed events on student will leave the Changed variable of the Student class as null and having no reference to call the Delegate on.
Posted Jul 2, 2008, 4:57 PM
Why is it necessary to check? because without if (Changed != null) program is working well.
Fernando SotoPosted Jul 2, 2008, 4:18 PM
In the Student class when the idNum or gpa is modified on the object they make a call to OnChanged method which checks to see if any user of the Student class has registered to receive Changed events, and if they have the OnChanged method will raise that event. This statement, if (Changed != null), checks to see if someone registered to receive Changed events. If Changed has a value of null no one has registered so do not even raise the event but if Changed is not null then someone want to know when those valuse have changed and so raise the event.
Note that this line of code registers to receive Changed event from the Student object.
stu.Changed += new ChangedEventHandler(StudentChanged);
Which is found in the EventListener class.
Fernando