RE: Problem: C# custom event is null
- From: "Mark R. Dawson" <MarkRDawson@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 25 Feb 2006 05:23:27 -0800
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[i] = 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
"CodeBlue" wrote:
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[i] = 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
- References:
- Problem: C# custom event is null
- From: CodeBlue
- Problem: C# custom event is null
- Prev by Date: Re: How to get relational data into an object?
- Next by Date: Re: newbie: object out of scope?
- Previous by thread: Problem: C# custom event is null
- Next by thread: Re: Problem: C# custom event is null
- Index(es):
Relevant Pages
|