473,320 Members | 1,719 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,320 software developers and data experts.

Problem with inherited events

Opa


Hi,

I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.
public class ClassA
{
public delegate void DialogAdded(object sender);
public event DialogAdded OnDialogAdded;
public void RaiseEventDialogAdded()
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(this);
}
}
}
public class ClassB: ClassA
{

}
objClassB = new ClassB();
objClassB.OnDialogAdded +=new ClassA.DialogAdded(objClassB_OnDialogAdded);
So I subsribe to the inherited event as shown above, but
when I debug the base class (ClassA) , the debugger returns OnDialogAdded
event as null.
Any ideas?

Thanks,

Opa


Nov 16 '05 #1
7 4857
Opa <Op*@discussions.microsoft.com> wrote:
I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.


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.

My guess is that you're subscribing to events with one instance, then
trying to fire events with another. I'll have to see more code to know
for sure though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Opa
Ok,

///
Here is my base class (Note: it is a seperate assembly from my derived class)
///
namespace TSC.VXMLEngine
{
/// <summary>
/// Summary description for Engine.
/// </summary>
public class VXmlEngine
{

private static volatile VXmlEngine _instance = null;
private static object syncRoot = new object();
public int iObjectID = 0;

//declare delegates (event handlers) for event subscribers
public delegate void DialogAdded(object sender);
//declare event for to be handled by the delegates
public event DialogAdded OnDialogAdded;

public VXmlEngine()
{
//
// TODO: Add constructor logic here
//
}

// public property that can only get the single instance of this class.
public static VXmlEngine Instance
{
get
{
// only create a new instance if one doesn't already exist.
if (_instance == null)
{
// use this lock to ensure that only one thread is access
// this block of code at once.
lock(syncRoot)
{
if (_instance == null)
_instance = new VXmlEngine(); //create new instance of this class
}
}
// return instance where it was just created or already existed.
return _instance;
}
}

public void RaiseEventDialogAdded(VXmlDialog dialog)
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(this, dialog);
}
}
}
}

///

//Here is my derived class

using System;
using System.IO;
using TSC.VXMLEngine;

namespace SurveyCast.Designer.Engine.Voice
{
/// <summary>
/// Summary description for VoiceEngine.
/// </summary>
public class VoiceEngine : VXmlEngine
{

private static volatile VoiceEngine _instance = null;
private static object syncRoot = new object();
public int iObjectID = 0;
// make the default constructor protected, so that no one can directly
create it.
protected VoiceEngine()
{
}

// public property that can only get the single instance of this class.
public static VoiceEngine Instance
{
get
{
// only create a new instance if one doesn't already exist.
if (_instance == null)
{
// use this lock to ensure that only one thread is access
// this block of code at once.
lock(syncRoot)
{
if (_instance == null)
_instance = new VoiceEngine(); //create new instance of this class
}
}
// return instance where it was just created or already existed.
return _instance;
}
}

}
}
//here is where I subscribe to the event:

_voiceEngine = VoiceEngine.Instance;
_voiceEngine.OnDialogAdded +=new
TSC.VXMLEngine.VXmlEngine.DialogAdded(_voiceEngine _OnDialogAdded);


"Jon Skeet [C# MVP]" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.


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.

My guess is that you're subscribing to events with one instance, then
trying to fire events with another. I'll have to see more code to know
for sure though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Hi

You are probably declaring the event in one class and invoking it in
another. This is not the proper way of handling inherited events.

As per the convention, add a virtual 'On'xxx virtual method in the base
class which would trigger the event, override it in the derived class if
required, and then call it.

Let me know if you need more help.

HTH,
Rakesh Rajan

"Opa" wrote:


Hi,

I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.
public class ClassA
{
public delegate void DialogAdded(object sender);
public event DialogAdded OnDialogAdded;
public void RaiseEventDialogAdded()
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(this);
}
}
}
public class ClassB: ClassA
{

}
objClassB = new ClassB();
objClassB.OnDialogAdded +=new ClassA.DialogAdded(objClassB_OnDialogAdded);
So I subsribe to the inherited event as shown above, but
when I debug the base class (ClassA) , the debugger returns OnDialogAdded
event as null.
Any ideas?

Thanks,

Opa



Nov 16 '05 #4
Opa
Actually, the base class is a singleton type and I should be able to invoke
it from another class. I don't want to override it because this would force
me to change my entire design.

Any other suggestions?
"Rakesh Rajan" wrote:
Hi

You are probably declaring the event in one class and invoking it in
another. This is not the proper way of handling inherited events.

As per the convention, add a virtual 'On'xxx virtual method in the base
class which would trigger the event, override it in the derived class if
required, and then call it.

Let me know if you need more help.

HTH,
Rakesh Rajan

"Opa" wrote:


Hi,

I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.
public class ClassA
{
public delegate void DialogAdded(object sender);
public event DialogAdded OnDialogAdded;
public void RaiseEventDialogAdded()
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(this);
}
}
}
public class ClassB: ClassA
{

}
objClassB = new ClassB();
objClassB.OnDialogAdded +=new ClassA.DialogAdded(objClassB_OnDialogAdded);
So I subsribe to the inherited event as shown above, but
when I debug the base class (ClassA) , the debugger returns OnDialogAdded
event as null.
Any ideas?

Thanks,

Opa



Nov 16 '05 #5
Opa <Op*@discussions.microsoft.com> wrote:
///
Here is my base class (Note: it is a seperate assembly from my derived class)
///


That's not a complete program. Please post something I can compile and
then run, which demonstrates the problem.

By the way, you might want to look at
http://www.pobox.com/~skeet/csharp/singleton.html for a rather simpler
implementation of the singleton pattern.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi Opa,

Overriding isn't necessary if your derived type doesn't have do some
processing when it needs to trigger the event.
The only difference you need to make are these: add the Onxxx method in the
base class, and then whenever you have to trigger the event, call that method
rather than the event directly.

Let me know if I could help more.

HTH,
Rakesh Rajan

"Opa" wrote:
Actually, the base class is a singleton type and I should be able to invoke
it from another class. I don't want to override it because this would force
me to change my entire design.

Any other suggestions?
"Rakesh Rajan" wrote:
Hi

You are probably declaring the event in one class and invoking it in
another. This is not the proper way of handling inherited events.

As per the convention, add a virtual 'On'xxx virtual method in the base
class which would trigger the event, override it in the derived class if
required, and then call it.

Let me know if you need more help.

HTH,
Rakesh Rajan

"Opa" wrote:


Hi,

I have a class (ClassB) that inherits from another class (ClassA) which has
a delegate and event.

I can see the events from ClassA in an intance object I create of ClassB.
I also subscribe to the event.

The problem is when I check go to "fire" the event in ClassA, the event
evaluates to null as if no one subscribed.
public class ClassA
{
public delegate void DialogAdded(object sender);
public event DialogAdded OnDialogAdded;
public void RaiseEventDialogAdded()
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(this);
}
}
}
public class ClassB: ClassA
{

}
objClassB = new ClassB();
objClassB.OnDialogAdded +=new ClassA.DialogAdded(objClassB_OnDialogAdded);
So I subsribe to the inherited event as shown above, but
when I debug the base class (ClassA) , the debugger returns OnDialogAdded
event as null.
Any ideas?

Thanks,

Opa



Nov 16 '05 #7
Opa
I will try your suggestion

Thank You

"Rakesh Rajan" wrote:
Hi Opa,

Overriding isn't necessary if your derived type doesn't have do some
processing when it needs to trigger the event.
The only difference you need to make are these: add the Onxxx method in the
base class, and then whenever you have to trigger the event, call that method
rather than the event directly.

Let me know if I could help more.

HTH,
Rakesh Rajan

"Opa" wrote:
Actually, the base class is a singleton type and I should be able to invoke
it from another class. I don't want to override it because this would force
me to change my entire design.

Any other suggestions?
"Rakesh Rajan" wrote:
Hi

You are probably declaring the event in one class and invoking it in
another. This is not the proper way of handling inherited events.

As per the convention, add a virtual 'On'xxx virtual method in the base
class which would trigger the event, override it in the derived class if
required, and then call it.

Let me know if you need more help.

HTH,
Rakesh Rajan

"Opa" wrote:

>
>
> Hi,
>
> I have a class (ClassB) that inherits from another class (ClassA) which has
> a delegate and event.
>
> I can see the events from ClassA in an intance object I create of ClassB.
> I also subscribe to the event.
>
> The problem is when I check go to "fire" the event in ClassA, the event
> evaluates to null as if no one subscribed.
>
>
> public class ClassA
> {
> public delegate void DialogAdded(object sender);
> public event DialogAdded OnDialogAdded;
> public void RaiseEventDialogAdded()
> {
> //if anyone has subsribed, then notify them
> if (OnDialogAdded != null)
> {
> OnDialogAdded(this);
> }
> }
> }
>
>
> public class ClassB: ClassA
> {
>
> }
>
>
> objClassB = new ClassB();
> objClassB.OnDialogAdded +=new ClassA.DialogAdded(objClassB_OnDialogAdded);
>
>
> So I subsribe to the inherited event as shown above, but
> when I debug the base class (ClassA) , the debugger returns OnDialogAdded
> event as null.
>
>
> Any ideas?
>
> Thanks,
>
> Opa
>
>
>
>
>
>
>
>

Nov 16 '05 #8

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

Similar topics

2
by: CMEDIA_SOUND | last post by:
I have a peculiar problem, I have a tabpage with a label control on it. When i set a background image to the tabpage and drag the label around it has paint issues in that it is slow, granted the...
1
by: Robert | last post by:
Hi, I've inherited the XmlDocument class to include some custom methods that I want to use on a particular XML file. I need to know whether the document has changed since being loaded, and I...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
2
by: Wiktor Zychla [C# MVP] | last post by:
Hi, suppose there are two interfaces that contain methods with the same name but different signature: interface I1 { void F(); } interface I2 { int F(); } it is then easy to implement...
10
by: Chad Miller | last post by:
I currently have a base form that I inherit. The base for has a custom event. The event will not raise threw the inherited form. I was wondering if events work threw inheritance or should I use...
1
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned...
2
by: MuZZy | last post by:
Hi, Consider i have this code: // --------------------------------------------------------------------- public delegate void DoSearchEventHandler(Form f, DoSearchEventArgs e); public class...
6
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.