T
Tony Johansson
Hello!
Here is a complete simple program example which consist of four classes.
There are two things in this example that are relevant for my question and
that is Timers and delegate. In this program example there is a Timer object
which has a interval set to
1 second. When this timer has elapse it will call the event handler
OnTimedEvent. In this method
handler OnTimedEvent it will call method Notify. In this method Notify it
will call the delegate method that
has been added to the delegate variable tickers. But if no method has been
added do the delegate variable it will be
null. So if I run the program in relese mode and don't add a method to the
delegete variable tickers it will be null but I don't get any
nullReferenceException. When I test and don't add a mehod to the delegete I
just modify the program
and comment out these two lines below
tickers += newMethod; //add method newMethod to delegete
tickers -= oldMethod; //remove method newMethod from delegate
The Timer will still function elapsing every second even if no method has
been added to the delegete variable tickers.
But now to the strange thing if I run the program in debug mode I get a
nullReference Exception.
So as summery why do I get a nullReferenceExcetion in Debug but no in
release. I would be satisfied if I got a nullReferenceException in both
release and debug mode.
I know that a check would be added if the tickers is null.
First you have the class Program below.
//Start class Program
//***************
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Delegates
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
//Start class Form1
//***************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Delegates
{
partial class Form1 : Form
{
private Clock clock;
public Form1()//C-tor
{
InitializeComponent();
clock = new Clock(digital);
}
private void start_Click(object sender, System.EventArgs e)
{
clock.Start();
}
private void stop_Click(object sender, System.EventArgs e)
{
clock.Stop();
}
}
}
//Start class Clock
//*************
using System.Windows.Forms;
namespace Delegates
{
class Clock
{
private Ticker pulsed = new Ticker();
private TextBox display;
public Clock(TextBox displayBox)//C-tor
{
display = displayBox;
}
public void Start()
{
pulsed.Add(RefreshTime); //Call add to add method
RefreshTime
}
public void Stop()
{
pulsed.Remove(RefreshTime);//call remove to remove method
RefreshTime
}
private void RefreshTime(int hh, int mm, int ss)
{
display.Text = string.Format("{02}:{12}:{22}", hh, mm,
ss);
}
}
}
//Start class Ticker
//*************
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
Here is a complete simple program example which consist of four classes.
There are two things in this example that are relevant for my question and
that is Timers and delegate. In this program example there is a Timer object
which has a interval set to
1 second. When this timer has elapse it will call the event handler
OnTimedEvent. In this method
handler OnTimedEvent it will call method Notify. In this method Notify it
will call the delegate method that
has been added to the delegate variable tickers. But if no method has been
added do the delegate variable it will be
null. So if I run the program in relese mode and don't add a method to the
delegete variable tickers it will be null but I don't get any
nullReferenceException. When I test and don't add a mehod to the delegete I
just modify the program
and comment out these two lines below
tickers += newMethod; //add method newMethod to delegete
tickers -= oldMethod; //remove method newMethod from delegate
The Timer will still function elapsing every second even if no method has
been added to the delegete variable tickers.
But now to the strange thing if I run the program in debug mode I get a
nullReference Exception.
So as summery why do I get a nullReferenceExcetion in Debug but no in
release. I would be satisfied if I got a nullReferenceException in both
release and debug mode.
I know that a check would be added if the tickers is null.
First you have the class Program below.
//Start class Program
//***************
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace Delegates
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
}
//Start class Form1
//***************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Delegates
{
partial class Form1 : Form
{
private Clock clock;
public Form1()//C-tor
{
InitializeComponent();
clock = new Clock(digital);
}
private void start_Click(object sender, System.EventArgs e)
{
clock.Start();
}
private void stop_Click(object sender, System.EventArgs e)
{
clock.Stop();
}
}
}
//Start class Clock
//*************
using System.Windows.Forms;
namespace Delegates
{
class Clock
{
private Ticker pulsed = new Ticker();
private TextBox display;
public Clock(TextBox displayBox)//C-tor
{
display = displayBox;
}
public void Start()
{
pulsed.Add(RefreshTime); //Call add to add method
RefreshTime
}
public void Stop()
{
pulsed.Remove(RefreshTime);//call remove to remove method
RefreshTime
}
private void RefreshTime(int hh, int mm, int ss)
{
display.Text = string.Format("{02}:{12}:{22}", hh, mm,
ss);
}
}
}
//Start class Ticker
//*************
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