473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Feb 25 '06 #1
3 11697
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

Feb 25 '06 #2
CodeBlue <mi*********@gmail.com> wrote:
(these are sample classes to illustrate)
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 25 '06 #3
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

Feb 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: T | last post by:
I have a web site i want to deploy using a VS.NEt set up project. I have no problems with that, that is fine and works no problem. But the web application uses a custom event log to log...
1
by: Victor | last post by:
I am developing a windows service application that uses a custom event. the problem is that the service installer associates the service with application log during the installation process....
2
by: Nicole - ASP/C# Beginner | last post by:
I am trying to kick off an custom event in a usercontrol that the webpage will listen and preform some actions if the event is fired from within my usercontrol
5
by: Dan Brill | last post by:
Hi, I'm sure this issue has been covered before but after searching around I can't find anything which deals with quite the same set of circumstances or suggests an optimal solution. The...
3
by: vvenk | last post by:
Hello: I came across the following code in C# that adds a custom event handler for triggering the click event on a button: System.Web.UI.WebControls.Button button; object buttonOnTab1 =...
0
by: Eniac | last post by:
Hello, I've started using Enterprise Library 2.0 recently and I've encountered a problem that seems to be ... well... undocumented :) Basically, when I set a Trace Listener (formatted event...
2
by: T McDonald | last post by:
I've been having a problem writing to an event to the Application log. I believe the issue is caused by the fact that the source was previously both a custom log and the source. I deleted the...
6
by: tshad | last post by:
I was looking at a page that showed how to set up a custom event and it seems to work ok. But I am not sure how I would use it. How would I subscribe to it. There is actual action (such as...
1
by: qwedster | last post by:
Howdy! I want to write a Custom Event Handler replicating textBox1_TextChanged event handler to update textBox2 with textBox1 text as I type. I tried the following in vain. Please help. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.