Null Object Reference when raising event in class constructor - c#

I'm having some issues here, I'm working with a class called Cell and when I create each cell I want to raise an event OnCellCreated which my IGameViewer will attach to eventually. For some weird reason it doesn't work though, now I've bypassed this by instead calling IGameViewer.DisplayCell in the constructor, but it's incredibly strange because it passes the exact same object reference from the constructor and it works, but when I try to do it with my event I will get a null object reference. So does anyone have any ideas?
Here's the code
class Cell
{
public delegate void CellChangedHandler(Cell cell);
#region Properties & Fields
private Mark markType = Mark.Empty;
private IGameViewer viewer;
public static event CellChangedHandler OnCellChanged;
public static event CellChangedHandler OnCellCreated;
public readonly Tuple<int, int> pos;
public Mark MarkType {
get { return markType; }
set
{
// Only allow changes to cells without a mark
if (markType.Equals(Mark.Empty))
{
markType = value;
OnCellChanged(this); //Model -> Viewer & Presenter, both can attach to this event
}
}
}
#endregion
#region Constructors
public Cell(IGameViewer viewer, Tuple<int, int> coords)
{
this.viewer = viewer;
this.pos = coords;
OnCellCreated(this); // <- This causes an object null reference exception to be thrown
viewer.DisplayCell(this); // <- This doesn't, even if I reverse the calling order
}
#endregion
}

Your OnCellCreated event is null, because noone yet subscribed to it. And how can caller do that if you call it already in construcutor. ? One can not subscribe to the event of the instance if the instance is not yet created (you are in constructor)
You can create a CellFactory class and add CellCretaed event to the factory. You ask CellFactory to create Cell instance, and CellFactory after creation of it, raises an event. Naturally you have to subscribe to that event before calling factory method.

Check if there're subscribers for the event whenever raising it:
public Cell(IGameViewer viewer, Tuple<int, int> coords) {
this.viewer = viewer;
...
if (!Object.ReferenceEquals(null, OnCellCreated))
OnCellCreated(this);
...
}

As #Tigran answered, the problem is that no one has yet subscribed to the event. What were you expecting to happen by invoking the event on the constructor?
If you had followed the pattern to implement events in .NET classes this would have never happened.
Change your code to this:
class Cell
{
private Mark markType = Mark.Empty;
private IGameViewer viewer;
public static event EventHandler CellChanged;
public readonly Tuple<int, int> pos;
public Cell(IGameViewer viewer, Tuple<int, int> coords)
{
this.viewer = viewer;
this.pos = coords;
viewer.DisplayCell(this);
}
public Mark MarkType {
get { return markType; }
set
{
// Only allow changes to cells without a mark
if (markType.Equals(Mark.Empty))
{
markType = value;
OnCellChanged();
}
}
}
protected virtual void OnCellChanged()
{
var handler = this.CellChanged;
if (handler)
{
handler(this, EventArgs.Empty);
}
}
}

Related

How to pass an event through classes in C#

Here are 3 C# classes :
class StartClass
{
event StartEvent;
// some code
class MidClass
{
private StartClass _startClass;
public MidClass (StartClass startClass)
{
_startClass = startClass;
}
// some code
class EndClass
{
private MidClass _midClass;
public EndClass (MidClass midClass)
{
_midClass = midClass;
}
// some code
public void OnStartEvent ()
{
// code to be executed on StartEvent called
}
What's best way if you want to attach EndClass.OnStartEvent listener to StartClass.StartEvent event ?
I think the best way would be to create a MidClass.StartEvent property referencing StartClass.StartEvent event in order to be able to attach the listener in EndClass doing _midClass.StartEvent =+ OnStartEvent;. Am I right ? How to attach StartClass.StartEvent to MidClass.StartEvent ?
In C# an event is much like a property. It is a wrapper for a delegate. Usually we are using auto-implemented events. But we can expand them. Properties have get and set accessors. Events have add and remove accessors.
class MidClass
{
private readonly StartClass _startClass;
public MidClass (StartClass startClass)
{
_startClass = startClass;
}
public event EventHandler StartEvent
{
add => _startClass.StartEvent += value;
remove => _startClass.StartEvent -= value;
}
}
Here, we create a StartEvent in MidClass that is a wrapper for the corresponding event in StartClass. StartClass.StartEvent must be public.
The advantage of this approach is that an event handler subscribing to MidClass.StartEvent will directly be attached to StartClass.StartEvent, with no intermediate call occurring when the event is risen.
One word to naming. Methods named OnEventName are usually used to raise events, while methods named PublisherName_EventName are used for event handlers.
class StartClass
{
public event EventHandler StartEvent;
private virtual void OnStartEvent()
{
StartEvent?Invoke(this, EventArgs.Empty);
}
}
class EndClass
{
private MidClass _midClass;
public EndClass (MidClass midClass)
{
_midClass = midClass;
_midClass.StartEvent += MidClass_StartEvent;
}
private void MidClass_StartEvent (object sender, EventArgs e)
{
// Code to be executed when StartEvent is triggered
}
}
I think the best way would be to create a MidClass.StartEvent property referencing StartClass.StartEvent event in order to be able to attach the listener in EndClass doing _midClass.StartEvent =+ OnStartEvent;. Am I right?
If you want to keep the reference to StartClass a private implementation detail of MidClass: Yes, implementing a "proxy" StartEvent in MidClass is the right way to do it.
How to attach StartClass.StartEvent to MidClass.StartEvent ?
By attaching a listener to _startClass.StartEvent which just raises the corresponding event in MidClass:
class MidClass
{
public event EventHandler StartEvent;
private readonly StartClass _startClass;
public MidClass(StartClass startClass)
{
_startClass = startClass;
_startClass.StartEvent += (sender, e) => this.StartEvent?.Invoke(this, e);
}
...
}
Note that I also added the readonly modifier to _startClass: If the value of _startClass changes during the lifetime of MidClass, you need to detach your event handler from the old reference and attach it to the new reference.

Assigning a delegate function to an EventHandler defined in an interface

I'm working with an interface IEventLogWatcher, its implementation EventLogWatcherWrapper, and a class that uses it all Subscriber. The issue I'm having revolves around assigning a delegate function to the EventHandler inside EventLogWatcherWrapper. My goal is to have the ReadEventLog() function get called when the EventRecordWritten event gets raised:
IEventLogWatcher.cs
public interface IEventLogWatcher : IDisposable {
event EventHandler<IEventRecordWrittenEventArgs> EventRecordWritten;
}
EventLogWatcherWrapper.cs
class EventLogWatcherWrapper : IEventLogWatcher {
private EventLogWatcher eventLogWatcher {get; set;}
public event EventHandler<IEventRecordWrittenEventArgs> WrapperEventRecordWritten;
public EventLogWatcherWrapper(EventLogQuery eventQuery){
eventLogWatcher = new EventLogWatcher(eventQuery);
}
event EventHandler<IEventRecordWrittenEventARgs> IEventLogWatcher.EventRecordWritten
{
add => WrapperEventRecordWritten += value;
remove => WrapperEventRecordWritten -= value;
}
}
Subscriber.cs
public class Subscriber{
IEventLogWatcher _watcher = new EventLogWatcherWrapper(someEventQuery);
public void someFunction(){
...
_watcher.EventRecordWritten += ReadEventLog;
// A breakpoint immediately after this statement reveals that _watcher.EventRecordWritten is still null even after this assignment
}
public void ReadEventLog(object obj, IEventRecordWrittenEventArgs arg){
// I expect this to get called when the EventRecordWritten event gets raised.
// This never gets called.
}
}
For some reason when I assign _watcher.EventRecordWritten += ReadEventLog;, I still see null for the EventRecordWritten field of _watcher so I suspect I am doing something wrong in EventLogWatcherWrapper in implementing the interface?

How to keep observing the dictionary?

I have dictionary like this :
public Dictionary<string, string> fields
{
get
{
}
set
{
}
}
How can I fire event which will keep listening to this dictionary?
And as soon as something is changed in the event some event handler should be called.
Is it possible to do it without making this dictionary observable?
private int _age;
public event System.EventHandler AgeChanged;
protected virtual void OnAgeChanged()
{
if (AgeChanged != null) AgeChanged(this,EventArgs.Empty);
}
public int Age
{
get
{
return _age;
}
set
{
_age=value;
OnAgeChanged();
}
}
I don't want the above implementation.
You'll need a custom class. Wrap your dictionary in a class and create an event to track when the dictionary changes. You will need to override the existing add / remove etc and when they get called, have them raise your event.
Here's a good question that may help.
.NET ObservableDictionary

How to create a Custom Routed Event ? WPF c#

I followed this tutorial but I couldn't apply what I learned to my project.
I have a LineGraph object (Dynamic Data Display) and I want to create an event that is raised when the thickness of the LineGraph is equal to 0.
How am I supposed to write it following this tutorial ?
Here is how I would do it with a RoutedEvent:
Create a class that derives from LineGraph, let's say CustomLineGraph:
public class CustomLineGraph : LineGraph {
}
Create our routed event like this:
public class CustomLineGraph : LineGraph {
public static readonly RoutedEvent ThicknessEvent = EventManager.RegisterRoutedEvent("Thickness", RoutingStrategy.Bubble, typeof(RoutedEventHandler, typeof(CustomLineGraph));
// .NET event wrapper
public event RoutedEventHandler Thickness
{
add { AddHandler(CustomLineGraph.ThicknessEvent, value); }
remove { RemoveHandler(CustomLineGraph.ThicknessEvent, value); }
}
}
Now we override the StrokeThickness property so we can raise our custom routed event when the value of that property is 0.
public class CustomLineGraph : LineGraph {
public static readonly RoutedEvent ThicknessEvent = EventManager.RegisterRoutedEvent("Thickness", RoutingStrategy.Bubble, typeof(RoutedEventHandler, typeof(CustomLineGraph));
// .NET event wrapper
public event RoutedEventHandler Thickness
{
add { AddHandler(CustomLineGraph.ThicknessEvent, value); }
remove { RemoveHandler(CustomLineGraph.ThicknessEvent, value); }
}
public override double StrokeThickness {
get { return base.StrokeThickness; }
set
{
base.StrokeThickness = value;
if (value == 0)
RaiseEvent(new RoutedEventArgs(CustomLineGraph.ThicknessEvent, this));
}
}
}
We are done !
Personally, I usually avoid creating events, preferring instead to create delegates. If there is some particular reason that you specifically need an event, then please ignore this answer. The reasons that I prefer to use delegates are that you don't need to create additional EventArgs classes and I can also set my own parameter types.
First, let's create a delegate:
public delegate void TypeOfDelegate(YourDataType dataInstance);
Now a getter and setter:
public TypeOfDelegate DelegateProperty { get; set; }
Now let's create a method that matches the in and out parameters of the delegate:
public void CanBeCalledAnything(YourDataType dataInstance)
{
// do something with the dataInstance parameter
}
Now we can set this method as one (of many) handlers for this delegate:
DelegateProperty += CanBeCalledAnything;
Finally, let's call our delegate... this is equivalent to raising the event:
if (DelegateProperty != null) DelegateProperty(dataInstanceOfTypeYourDataType);
Note the important check for null. So that's it! If you want more or less parameters, just add or remove them from the delegate declaration and the handling method... simple.

How can I clear event subscriptions in C#?

Take the following C# class:
c1 {
event EventHandler someEvent;
}
If there are a lot of subscriptions to c1's someEvent event and I want to clear them all, what is the best way to achieve this? Also consider that subscriptions to this event could be/are lambdas/anonymous delegates.
Currently my solution is to add a ResetSubscriptions() method to c1 that sets someEvent to null. I don't know if this has any unseen consequences.
From within the class, you can set the (hidden) variable to null. A null reference is the canonical way of representing an empty invocation list, effectively.
From outside the class, you can't do this - events basically expose "subscribe" and "unsubscribe" and that's it.
It's worth being aware of what field-like events are actually doing - they're creating a variable and an event at the same time. Within the class, you end up referencing the variable. From outside, you reference the event.
See my article on events and delegates for more information.
Add a method to c1 that will set 'someEvent' to null.
public class c1
{
event EventHandler someEvent;
public ResetSubscriptions() => someEvent = null;
}
class c1
{
event EventHandler someEvent;
ResetSubscriptions() => someEvent = delegate { };
}
It is better to use delegate { } than null to avoid the null ref exception.
The best practice to clear all subscribers is to set the someEvent to null by adding another public method if you want to expose this functionality to outside. This has no unseen consequences. The precondition is to remember to declare SomeEvent with the keyword 'event'.
Please see the book - C# 4.0 in the nutshell, page 125.
Some one here proposed to use Delegate.RemoveAll method. If you use it, the sample code could follow the below form. But it is really stupid. Why not just SomeEvent=null inside the ClearSubscribers() function?
public void ClearSubscribers ()
{
SomeEvent = (EventHandler) Delegate.RemoveAll(SomeEvent, SomeEvent);
// Then you will find SomeEvent is set to null.
}
Setting the event to null inside the class works. When you dispose a class you should always set the event to null, the GC has problems with events and may not clean up the disposed class if it has dangling events.
You can achieve this by using the Delegate.Remove or Delegate.RemoveAll methods.
Conceptual extended boring comment.
I rather use the word "event handler" instead of "event" or "delegate". And used the word "event" for other stuff. In some programming languages (VB.NET, Object Pascal, Objective-C), "event" is called a "message" or "signal", and even have a "message" keyword, and specific sugar syntax.
const
WM_Paint = 998; // <-- "question" can be done by several talkers
WM_Clear = 546;
type
MyWindowClass = class(Window)
procedure NotEventHandlerMethod_1;
procedure NotEventHandlerMethod_17;
procedure DoPaintEventHandler; message WM_Paint; // <-- "answer" by this listener
procedure DoClearEventHandler; message WM_Clear;
end;
And, in order to respond to that "message", a "event handler" respond, whether is a single delegate or multiple delegates.
Summary:
"Event" is the "question", "event handler (s)" are the answer (s).
Remove all events, assume the event is an "Action" type:
Delegate[] dary = TermCheckScore.GetInvocationList();
if ( dary != null )
{
foreach ( Delegate del in dary )
{
TermCheckScore -= ( Action ) del;
}
}
This is my solution:
public class Foo : IDisposable
{
private event EventHandler _statusChanged;
public event EventHandler StatusChanged
{
add
{
_statusChanged += value;
}
remove
{
_statusChanged -= value;
}
}
public void Dispose()
{
_statusChanged = null;
}
}
You need to call Dispose() or use using(new Foo()){/*...*/} pattern to unsubscribe all members of invocation list.
Instead of adding and removing callbacks manually and having a bunch of delegate types declared everywhere:
// The hard way
public delegate void ObjectCallback(ObjectType broadcaster);
public class Object
{
public event ObjectCallback m_ObjectCallback;
void SetupListener()
{
ObjectCallback callback = null;
callback = (ObjectType broadcaster) =>
{
// one time logic here
broadcaster.m_ObjectCallback -= callback;
};
m_ObjectCallback += callback;
}
void BroadcastEvent()
{
m_ObjectCallback?.Invoke(this);
}
}
You could try this generic approach:
public class Object
{
public Broadcast<Object> m_EventToBroadcast = new Broadcast<Object>();
void SetupListener()
{
m_EventToBroadcast.SubscribeOnce((ObjectType broadcaster) => {
// one time logic here
});
}
~Object()
{
m_EventToBroadcast.Dispose();
m_EventToBroadcast = null;
}
void BroadcastEvent()
{
m_EventToBroadcast.Broadcast(this);
}
}
public delegate void ObjectDelegate<T>(T broadcaster);
public class Broadcast<T> : IDisposable
{
private event ObjectDelegate<T> m_Event;
private List<ObjectDelegate<T>> m_SingleSubscribers = new List<ObjectDelegate<T>>();
~Broadcast()
{
Dispose();
}
public void Dispose()
{
Clear();
System.GC.SuppressFinalize(this);
}
public void Clear()
{
m_SingleSubscribers.Clear();
m_Event = delegate { };
}
// add a one shot to this delegate that is removed after first broadcast
public void SubscribeOnce(ObjectDelegate<T> del)
{
m_Event += del;
m_SingleSubscribers.Add(del);
}
// add a recurring delegate that gets called each time
public void Subscribe(ObjectDelegate<T> del)
{
m_Event += del;
}
public void Unsubscribe(ObjectDelegate<T> del)
{
m_Event -= del;
}
public void Broadcast(T broadcaster)
{
m_Event?.Invoke(broadcaster);
for (int i = 0; i < m_SingleSubscribers.Count; ++i)
{
Unsubscribe(m_SingleSubscribers[i]);
}
m_SingleSubscribers.Clear();
}
}

Categories