Hi Guys
NP110 Why is it significant
Why is it significant to check if (Changed != null)?. Because without this check
Program is executing well. Please explain the reason.
Thank you
// events1.cs
using System;
namespace MyCollections
{
using System.Collections;
// A delegate type for hooking up change notifications.
public delegate void ChangedEventHandler(object sender, EventArgs e);
//carry the event data
// A class that works just like ArrayList, but sends event
// notifications whenever the list changes.
public class ListWithChangedEvent : ArrayList
{
// An event that clients can use to be notified whenever the
// elements of the list change.
public event ChangedEventHandler Changed;
// Invoke the Changed event; called whenever list changes
protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
Changed(this, e);
}
// Override some of the methods that can change the list;
// invoke event after each
public override int Add(object value)
{
int i = base.Add(value);
OnChanged(EventArgs.Empty);
return i;
}
public override void Clear()
{
base.Clear();
OnChanged(EventArgs.Empty);
}
public override object this[int index]
{
set
{
base[index] = value;
OnChanged(EventArgs.Empty);
}
}
}
}
namespace TestEvents
{
using MyCollections;
class EventListener
{
private ListWithChangedEvent List;
public EventListener(ListWithChangedEvent list)
{
List = list;
// Add "ListChanged" to the Changed event on "List".
List.Changed += new ChangedEventHandler(ListChanged);
}
// This will be called whenever the list changes.
private void ListChanged(object sender, EventArgs e)
{
Console.WriteLine("This is called when the event fires.");
}
public void Detach()
{
// Detach the event and delete the list
List.Changed -= new ChangedEventHandler(ListChanged);
List = null;
}
}
class Test
{
// Test the ListWithChangedEvent class.
public static void Main()
{
// Create a new list.
ListWithChangedEvent list = new ListWithChangedEvent();
// Create a class that listens to the list's change event.
EventListener listener = new EventListener(list);
// Add and remove items from the list.
list.Add("item 1");
list.Clear();
listener.Detach();
}
}
}
/*
This is called when the event fires.
This is called when the event fires.
*/
Posted Jul 29, 2008, 7:02 PM
Thank you for your explanations.
AlanPosted Jul 29, 2008, 4:22 PM
That line is important because it checks to see whether any eventhandlers have been added to the Changed event. Changed will be null if they haven't (its default value) and so when you call Changed(this, e) a null reference exception will be thrown.
If you comment out this particular line:
if (Changed != null)
and also this one:
List.Changed += new ChangedEventHandler(ListChanged);
so that there are no eventhandlers attached to the event, then recompile and run, you'll see what I mean.
EDIT: Sorry got interrupted whilst making this post and didn't see that Ryan had posted in the meantime.
Ryan AlfordPosted Jul 29, 2008, 4:14 PM
it's always good practice to be defensive in your coding. Always assume values will be null/empty and code around it. Not doing it opens yourself up to unhandled exceptions that crash your application.