473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Trouble with event

I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in a
arrayList. Every time I create an instance, I say it witch is the handler of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?
Thank you very much in advance.
Nov 16 '05 #1
7 1133
Alberto wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in a
arrayList. Every time I create an instance, I say it witch is the handler of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?
Thank you very much in advance.

Has arrayA[i] been initialised to an instance of A?
It sounds like you are raising the event when no-one is subscribed to it.
The accepted methodology I have seen is to provide a private
On[EventName] method to call instead of your event which will check to
see if the event has been instanciated.
ie
private void OnModified()
{
if(Modified != null)
{
Modified(this);
}
}

This way the event wont be raised when no, one has a reference to it.
HTH
JB
Nov 16 '05 #2
Hi Alberto,

Are the instance of A properly instantiated?

HTH,
Rakesh Rajan

"Alberto" wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in a
arrayList. Every time I create an instance, I say it witch is the handler of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?
Thank you very much in advance.

Nov 16 '05 #3
The Last Gunslinger wrote:
Alberto wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them
in a
arrayList. Every time I create an instance, I say it witch is the
handler of the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error
says that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?
Thank you very much in advance.

Has arrayA[i] been initialised to an instance of A?


This is misleading sorry, as if it had not been you would get an error
when trying to add the event handler.
Are you setting arrayA[i] anywhere else as the new object will not have
the events wired up?

HTH
JB
Nov 16 '05 #4
Yes, I think so. More or less, the code is somethink like this:
ArrayList arrayA = new ArrayList();
....
arrayA.Add(A.LoadA());

A.LoadA() is a static method of the class A who returns an instance of the
class.

Thank your for your help.

"Rakesh Rajan" <Ra*********@discussions.microsoft.com> escribió en el
mensaje news:14**********************************@microsof t.com...
Hi Alberto,

Are the instance of A properly instantiated?

HTH,
Rakesh Rajan

"Alberto" wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in
a
arrayList. Every time I create an instance, I say it witch is the handler
of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?
Thank you very much in advance.

Nov 16 '05 #5
Alberto <al*****@nospam.com> wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in a
arrayList. Every time I create an instance, I say it witch is the handler of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

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

public class A
{
public delegate void Evento(object sender);
public event Evento Modified;

public static ArrayList LoadA()
{
SqlDataReader dr = readA();
ArrayList arrayTemp = new ArrayList();
while (dr.read())
arrayTemp.Add(A.LoadInstance(dr["idA"]);

return arrayTemp;
}

private static SqlDataReader ReadA()
{
SqlDataReader dr = Data.GetData("ReadA"); // Data is an object who can
acces to Sql server
return dr;
}

private static A LoadInstance()
{
SqlDataReader dr = Data.GetData("ReadA"); // Data is an object who can
acces to Sql server

A instance = new A();
A.property1 = dr["field"];
...
return instance;
}

public void Change()
{
// save changes in the data base
Modified(this);
}
}
In a form, where the user insert, delete, change and so on the data, I
insert the
instances of the object of type A in a arrayList in this way:

private void GetA()
{
arrayA = A.LoadA();
for (int i=0; i<arrayA.Count; i++)
{
((A)arrayA[i]).Modified += new A.Evento(frmA_Modified);
}
}

I hope you could understand it. Thank you very much.

"Jon Skeet [C# MVP]" <sk***@pobox.com> escribió en el mensaje
news:MP************************@msnews.microsoft.c om...
Alberto <al*****@nospam.com> wrote:
I have a class A who send an event when an instance is modified.
So, I declared the event in this way:

public delegate void Evento(object sender);
public event Evento Modified;

To throw the event I wrote this sentence:

Modified(this);

I have a form who works with instances of the class A so I stored them in
a
arrayList. Every time I create an instance, I say it witch is the handler
of
the
event in this way:

((A)arrayA[i]).Modified +=
new A.Evento(frmA_Modified);

The problem is when the event is raised from the class A. The error says
that
there is a reference to an object not established as an object instance.

I don't understand what's happening. Could anyone help me?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

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

Nov 16 '05 #7
Alberto <al*****@nospam.com> wrote:
This is the class A


<snip>

See http://www.pobox.com/~skeet/csharp/incomplete.html

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

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

Similar topics

14
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events. The method definitions for EventManager have...
0
by: Manuel D. Jim?nez | last post by:
Hi, we are developing an ActiveX control which plays a video streaming. We have used Visual Studio .net wizard to develop almost all the control interface, however we have trouble with event...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
4
by: bcallnan | last post by:
Hello All- I am trying to reference a subform's afterupdate event that is 3 deep and am having some trouble getting it to work. The control is a combo box and i cannot seem to trigger the...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.