473,480 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delegates and NullReferenceException

Hello All,

In the following code I was expecting to get a NullReferenceException,
but that didn't happen. I was hoping someone could explain why.

I have two classes, Form1 and Ticker. As soon as Form1 is loaded I am
expecting the Timer's elapsed event, ticking_Elapsed, to happen once
every second and for a NullReferenceException to occur since ticking is
not initialized, but nothing happens. I added the
MessageBox.Show("Done") to check if the ticking_Elapsed event really
was happening. The interesting part is that the message box does not
appear unless I comment out the code that calls the private Notify
method.

Why is there no NullReferenceException?

Any help is appreciated. Thank you.

Paul

public partial class Form1 : Form
{
private Ticker pulsed = new Ticker();

public Form1()
{
InitializeComponent();
}
}

class Ticker
{
public delegate void Tick(int hh, int mm, int ss);

private Tick tickers;
private Timer ticking = new Timer();

public Ticker()
{
this.ticking.Elapsed += new
ElapsedEventHandler(ticking_Elapsed);
this.ticking.Interval = 1000;
this.ticking.Enabled = true;
}

private void ticking_Elapsed(object sender, ElapsedEventArgs e)
{
Notify(1, 2, 3);
System.Windows.Forms.MessageBox.Show("Done");
}

public void Add(Tick newMethod)
{
this.tickers += newMethod;
}

public void Remove(Tick oldMethod)
{
this.tickers -= oldMethod;
}

private void Notify(int hours, int minutes, int seconds)
{
this.tickers(hours, minutes, seconds);
}
}

Jan 9 '07 #1
1 2910
Paul,

It depends how you use the timer object (Ticker in your case) If the tick
even is rasied in a saparate thread, which is the default behavior you
probably won't get the exception as exceptions does not cross thread
boundaries. What you can do is to use synchornizing object (look at
Timer.SynchornizingObject property) in order to marshal back the elapsed
event to the main UI thread. Then you will probably get the exception.
--
Stoitcho Goutsev (100)

"Paul" <he********@yahoo.cawrote in message
news:11**********************@i15g2000cwa.googlegr oups.com...
Hello All,

In the following code I was expecting to get a NullReferenceException,
but that didn't happen. I was hoping someone could explain why.

I have two classes, Form1 and Ticker. As soon as Form1 is loaded I am
expecting the Timer's elapsed event, ticking_Elapsed, to happen once
every second and for a NullReferenceException to occur since ticking is
not initialized, but nothing happens. I added the
MessageBox.Show("Done") to check if the ticking_Elapsed event really
was happening. The interesting part is that the message box does not
appear unless I comment out the code that calls the private Notify
method.

Why is there no NullReferenceException?

Any help is appreciated. Thank you.

Paul

public partial class Form1 : Form
{
private Ticker pulsed = new Ticker();

public Form1()
{
InitializeComponent();
}
}

class Ticker
{
public delegate void Tick(int hh, int mm, int ss);

private Tick tickers;
private Timer ticking = new Timer();

public Ticker()
{
this.ticking.Elapsed += new
ElapsedEventHandler(ticking_Elapsed);
this.ticking.Interval = 1000;
this.ticking.Enabled = true;
}

private void ticking_Elapsed(object sender, ElapsedEventArgs e)
{
Notify(1, 2, 3);
System.Windows.Forms.MessageBox.Show("Done");
}

public void Add(Tick newMethod)
{
this.tickers += newMethod;
}

public void Remove(Tick oldMethod)
{
this.tickers -= oldMethod;
}

private void Notify(int hours, int minutes, int seconds)
{
this.tickers(hours, minutes, seconds);
}
}

Jan 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
22851
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
2210
by: am72de | last post by:
Hi all, I have a delegate of type OracleRowUpdatedEventArgs which is inherited from System.Data.Common.RowUpdatedEventArgs and I have the OleDbRowUpdatedEventArgs which is inherited from...
2
7766
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
2
1628
by: Mark Olbert | last post by:
First off, the sympathy is for all you poor buggers out there who have to figure out how to marry Managed Extensions for C++ onto your legacy code. My condolences; my brief experience with the...
0
4749
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
2629
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
3400
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
1876
by: Scott Gifford | last post by:
I have a question about concurrency and delegates. Say I have a class roughly like this: public class Class1 { public delegate void MyEventHandler(); private event MyEventHandler onMyEvent;...
69
5512
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.