T
Tony Johansson
Hello!
Assume I have some classes and in this case the Ticker class below is
relevant for my question.
I have a Timer instance in this class with interval set to 1 second. When
the timer elapse efter each second it
will call the method handler OnTimeEvent. I have also a delegete declaration
here where the delegete type is Tick.
If no method has been added to the delegate variable tickers then tickers is
null.
Now to my question: When the eventhandler Notify is called as a result of
the timer has elapse and no method has been
added to the delegete variable then tickers is null. But when I run this
program is release I didn't get any NullReferenceException.when this method
was called "tickers(hours, minutes, seconds); " see below.
If I insted run the program in debug mode I get a nullReferenceException.
Can somebody explain that?
One more thing is it a good idea to check if tickes is null?
namespace Delegates
{
using System.Timers;
class Ticker
{
public delegate void Tick(int hh, int mm, int ss); //delegate
declaration
private Tick tickers; // delegate variable
private Timer ticking = new Timer(); // instansiate Timer
public Ticker() //C-tor
{
ticking.Elapsed += new ElapsedEventHandler(OnTimedEvent); //
eventhandler to be called when timer elapse
ticking.Interval = 1000; // 1 second interval
ticking.Enabled = true;
}
public void Add(Tick newMethod) // delegete type
{
//tickers += newMethod; //add method newMethod to delegete
}
public void Remove(Tick oldMethod) //delegete type
{
//tickers -= oldMethod; //remove method newMethod from delegate
}
private void Notify(int hours, int minutes, int seconds)
{
tickers(hours, minutes, seconds); //call hidden delegate method
}
//eventhandler that is called when timer is elapsing
private void OnTimedEvent(object source, ElapsedEventArgs args)
{
int hh = args.SignalTime.Hour;
int mm = args.SignalTime.Minute;
int ss = args.SignalTime.Second;
Notify(hh, mm, ss); // call notify
}
}
}
///Tony
Assume I have some classes and in this case the Ticker class below is
relevant for my question.
I have a Timer instance in this class with interval set to 1 second. When
the timer elapse efter each second it
will call the method handler OnTimeEvent. I have also a delegete declaration
here where the delegete type is Tick.
If no method has been added to the delegate variable tickers then tickers is
null.
Now to my question: When the eventhandler Notify is called as a result of
the timer has elapse and no method has been
added to the delegete variable then tickers is null. But when I run this
program is release I didn't get any NullReferenceException.when this method
was called "tickers(hours, minutes, seconds); " see below.
If I insted run the program in debug mode I get a nullReferenceException.
Can somebody explain that?
One more thing is it a good idea to check if tickes is null?
namespace Delegates
{
using System.Timers;
class Ticker
{
public delegate void Tick(int hh, int mm, int ss); //delegate
declaration
private Tick tickers; // delegate variable
private Timer ticking = new Timer(); // instansiate Timer
public Ticker() //C-tor
{
ticking.Elapsed += new ElapsedEventHandler(OnTimedEvent); //
eventhandler to be called when timer elapse
ticking.Interval = 1000; // 1 second interval
ticking.Enabled = true;
}
public void Add(Tick newMethod) // delegete type
{
//tickers += newMethod; //add method newMethod to delegete
}
public void Remove(Tick oldMethod) //delegete type
{
//tickers -= oldMethod; //remove method newMethod from delegate
}
private void Notify(int hours, int minutes, int seconds)
{
tickers(hours, minutes, seconds); //call hidden delegate method
}
//eventhandler that is called when timer is elapsing
private void OnTimedEvent(object source, ElapsedEventArgs args)
{
int hh = args.SignalTime.Hour;
int mm = args.SignalTime.Minute;
int ss = args.SignalTime.Second;
Notify(hh, mm, ss); // call notify
}
}
}
///Tony