By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Referencing "this" in event calls

P: n/a
When I reference "this" in call to event, I get the following error:

An unhandled exception of type 'System.NullReferenceException'
occurred in csbasesockets.dll

Additional information: Object reference not set to an instance of an
object.
Here is a summary of my code:

public delegate void TCPConnectionEventHandler(TCPConnection sender);

public class TCPConnection
{

public TCPConnection(){}

public event TCPCOnnectionEventHandler OnConnected;

public void Connect()
{
//some code to connect here...
this.OnConnected(this); <-- Error received here
}
}

public class frmClient: System.Windows.Forms.Form

{
public frmClient()
{
m_client = new TCPConnection();
m_client.OnConnected+= new
TCPConnectionEventHandler(this.client_OnConnected) ;
m_client.Connect();
}

private void client_OnConnected(TCPIPConnection sender)
{
//do something here...
}
}

When I hover over "this" there is information but when I hover over
"OnConnected" I can see "OnConnected = <undefined value>".

Thanks in advance...
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Richard,

OnConnected is an event and it doesn't have any delegates hooked up to it
yet. Here is how you should raise events:

if (OnConnected != null)
{
this.OnConnected(this);
}

Once a delegate is added to OnConnected, it will no longer evaluate to null.

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com

"Richard" <kh******@hotmail.com> wrote in message
news:ee**************************@posting.google.c om...
When I reference "this" in call to event, I get the following error:

An unhandled exception of type 'System.NullReferenceException'
occurred in csbasesockets.dll

Additional information: Object reference not set to an instance of an
object.
Here is a summary of my code:

public delegate void TCPConnectionEventHandler(TCPConnection sender);

public class TCPConnection
{

public TCPConnection(){}

public event TCPCOnnectionEventHandler OnConnected;

public void Connect()
{
//some code to connect here...
this.OnConnected(this); <-- Error received here
}
}

public class frmClient: System.Windows.Forms.Form

{
public frmClient()
{
m_client = new TCPConnection();
m_client.OnConnected+= new
TCPConnectionEventHandler(this.client_OnConnected) ;
m_client.Connect();
}

private void client_OnConnected(TCPIPConnection sender)
{
//do something here...
}
}

When I hover over "this" there is information but when I hover over
"OnConnected" I can see "OnConnected = <undefined value>".

Thanks in advance...

Nov 16 '05 #2

P: n/a
Joe Mayo [C# MVP] <jm***@nospamAtCSharpDashStation.com> wrote:
OnConnected is an event and it doesn't have any delegates hooked up to it
yet. Here is how you should raise events:

if (OnConnected != null)
{
this.OnConnected(this);
}

Once a delegate is added to OnConnected, it will no longer evaluate to null.


It depends on how thread-safe you want things to be, however. I have a
section on making events thread-safe at
http://www.pobox.com/~skeet/csharp/m...ml#lock.choice

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Joe Mayo [C# MVP] <jm***@nospamAtCSharpDashStation.com> wrote:
OnConnected is an event and it doesn't have any delegates hooked up to it yet. Here is how you should raise events:

if (OnConnected != null)
{
this.OnConnected(this);
}

Once a delegate is added to OnConnected, it will no longer evaluate to
null.
It depends on how thread-safe you want things to be, however. I have a
section on making events thread-safe at
http://www.pobox.com/~skeet/csharp/m...ml#lock.choice


That's a pretty good article. What are your thoughts on the risk of someone
doing?:

someEvent = new SomeEventHandler(SomeMethod);

Since the someEvent delegate is private, I'm thinking the risk is minimal,
but am interested in what you think.

Joe
Nov 16 '05 #4

P: n/a
Joe Mayo [C# MVP] <jmayo@.n0-sP> wrote:
It depends on how thread-safe you want things to be, however. I have a
section on making events thread-safe at
http://www.pobox.com/~skeet/csharp/m...ml#lock.choice


That's a pretty good article. What are your thoughts on the risk of someone
doing?:

someEvent = new SomeEventHandler(SomeMethod);

Since the someEvent delegate is private, I'm thinking the risk is minimal,
but am interested in what you think.


And doing that outside a lock? Yes, that would be dangerous just as any
unsynchronized access to shared data is dangerous. As you say though,
there's no danger *outside* the class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.