Trouble with event

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

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).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.
 
Alberto said:
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).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 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
 
The said:
Alberto said:
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).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 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 anywhere else as the new object will not have
the events wired up?

HTH
JB
 
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 said:
Hi Alberto,

Are the instance of A properly instantiated?

HTH,
Rakesh Rajan

Alberto said:
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).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.
 
Alberto said:
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).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.
 
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).Modified += new A.Evento(frmA_Modified);
}
}

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



Jon Skeet said:
Alberto said:
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).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.
 
Back
Top