Problem: C# custom event is null

C

CodeBlue

Hi,

(these are sample classes to illustrate)


I have 3 classes:


public class A
{
....
//array of 4 B objects
public B[4] B_Obj;
public A()
{
B_Obj = new B[4];
for (int i=0; i<4; i++)
B_Obj = new B();



}


public class B
{
public delegate void MyDelegate(int i);
public event MyDelegate OnFire;

public void ChangeValue(int x)
{
if (OnFire != null)
OnFire(x);
}


public class C
{
public A[] a = new A[10];


public C()
{
for (int x=0; x < 10; x++)
{
//init the A objects
A[x] = new A();
for (int y=0; y<4; y++)
{
//link all the B object events to one method
A[x].B_Obj [y].OnFire += new B.MyDelegate(B_Event_Raised);

}
}


//try to fire the event
A[0].B_Obj[0].ChangeValue(1);


}//end C constructor


public void B_Event_Raised(int x)
{
....
}


}


///////////////////////////////////////////////

However, in the above code, B_Event_Raised never gets called, and the
OnFire event always returns null. How come this is the case, when the
event handler is linked in C???


Regards,


Alex
 
G

Guest

Hi CodeBlue,
your code works just as epxected when I ran it, the B_event_raised method
is getting called. Here is your code modified to compile from the original
example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
C c = new C();
}
}


public class A
{
//array of 4 B objects
public B[] B_Obj;
public A()
{
B_Obj = new B[4];
for (int i = 0; i < 4; i++)
B_Obj = new B();



}
}


public class B
{
public delegate void MyDelegate(int i);
public event MyDelegate OnFire;

public void ChangeValue(int x)
{
if (OnFire != null)
OnFire(x);
}
}


public class C
{
public A[] a = new A[10];


public C()
{
for (int x = 0; x < 10; x++)
{
//init the A objects
a[x] = new A();
for (int y = 0; y < 4; y++)
{
//link all the B object events to one method
a[x].B_Obj[y].OnFire += new B.MyDelegate(B_Event_Raised);

}
}


//try to fire the event
a[0].B_Obj[0].ChangeValue(1);


}//end C constructor


public void B_Event_Raised(int x)
{

}
}

}


Mark Dawson
http://www.markdawson.org
 
J

Jon Skeet [C# MVP]

CodeBlue said:
(these are sample classes to illustrate)

However, in the above code, B_Event_Raised never gets called, and the
OnFire event always returns null. How come this is the case, when the
event handler is linked in C???

Well, it looks okay to 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.
 
C

CodeBlue

Thanks Jon and Mark,

As your "Short but Complete" page says Jon, after I put the program in
the compiler, I immediately saw the difference from my original
program, and I felt foolish to have asked the question (hey, it was 4
am) ;)

The problem is that I was calling the ChangeValue function on a
different B_Obj, not the one I added the event handler for.

Best Regards,

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top