Problem with inherited events

G

Guest

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
 
J

Jon Skeet [C# MVP]

Opa said:
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.
 
G

Guest

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);
 
G

Guest

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
 
G

Guest

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?
 
G

Guest

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
 
G

Guest

I will try your suggestion

Thank You

Rakesh Rajan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top