Problem: C# custom event is null

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CodeBlue

    #1

    Problem: C# custom event is null

    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

  • Mark R. Dawson

    #2
    RE: Problem: C# custom event is null

    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.Collecti ons.Generic;
    using System.Text;

    namespace ConsoleApplicat ion19
    {
    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



    "CodeBlue" wrote:
    [color=blue]
    > 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
    >
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Problem: C# custom event is null

      CodeBlue <minimaximus@gm ail.com> wrote:[color=blue]
      > (these are sample classes to illustrate)[/color]

      <snip>
      [color=blue]
      > 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???[/color]

      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.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • CodeBlue

        #4
        Re: Problem: C# custom event is null

        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

        Comment

        Working...