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.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


.



Relevant Pages

  • Re: How to use generics?
    ... getting rid of the ACollection and BCollection and just having Collectionwould be a good start towards using generics to the full extent. ... public void AddCollection() ... public bool ContainsKey ...
    (microsoft.public.dotnet.languages.csharp)
  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Tricky form problem
    ... > ShowDialog method) different modal child forms. ... when using ShowDialog() there's no ... > public class MainForm: Form ... > public void ShowForm() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: InvalidOperationException was unhandled
    ... > public Vehicle MyVehicle; ... >public class Vehicle ... > public void SerializeOriginal(String filename) ...
    (microsoft.public.dotnet.vjsharp)
  • RE: SoapTypeAttribute (System.Xml.Serialization) Example Code Fails
    ... "David Bozzini" wrote: ... > public class Employee{ ... > public void SerializeOriginal{ ... > mySerializer.Serialize(writer, myGroup); ...
    (microsoft.public.dotnet.framework)