By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,983 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,983 IT Pros & Developers. It's quick & easy.

Inheriting events from singleton class problem for two days

P: n/a
Opa
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.

The event in the singleton class always evaluates to null even when I cleary
subscribe to it.

Does anyone have some sample code of how to do this, or is this not possible.

I can post my code if needed.

Please help!
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Opa <Op*@discussions.microsoft.com> wrote:
I have tried for two days to solve this problem with no luck.
Any reason for posting a new thread rather than continuing in the
previous one?

<snip>
I can post my code if needed.


Yes, please do. I've already asked you to post a short but complete
program which demonstrates the problem...

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

P: n/a
Opa
Hi Jon,
I wasn't sure if it would get lost in the stack. Yes, I am still having
this problem.
I've tried for hours to figure it out. I'm still no where, can you help.

Thanks
"Jon Skeet [C# MVP]" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
I have tried for two days to solve this problem with no luck.


Any reason for posting a new thread rather than continuing in the
previous one?

<snip>
I can post my code if needed.


Yes, please do. I've already asked you to post a short but complete
program which demonstrates the problem...

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

P: n/a
Opa <Op*@discussions.microsoft.com> wrote:
I wasn't sure if it would get lost in the stack.
It won't. New posts will always show up as new posts.
Yes, I am still having this problem.
I've tried for hours to figure it out. I'm still no where, can you help.


Certainly. Just post a short but complete program which demonstrates
the problem, and I'm sure it won't be too hard to fix.

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

P: n/a
Hi Opa,

Please post the code.

Regards,
Rakesh Rajan

"Opa" wrote:
Hi Jon,
I wasn't sure if it would get lost in the stack. Yes, I am still having
this problem.
I've tried for hours to figure it out. I'm still no where, can you help.

Thanks
"Jon Skeet [C# MVP]" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
I have tried for two days to solve this problem with no luck.


Any reason for posting a new thread rather than continuing in the
previous one?

<snip>
I can post my code if needed.


Yes, please do. I've already asked you to post a short but complete
program which demonstrates the problem...

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

Nov 16 '05 #5

P: n/a
Opa
Hi Rekesh,

See the reply I posted to Jon with my sample code.

"Rakesh Rajan" wrote:
Hi Opa,

Please post the code.

Regards,
Rakesh Rajan

"Opa" wrote:
Hi Jon,
I wasn't sure if it would get lost in the stack. Yes, I am still having
this problem.
I've tried for hours to figure it out. I'm still no where, can you help.

Thanks
"Jon Skeet [C# MVP]" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
> I have tried for two days to solve this problem with no luck.

Any reason for posting a new thread rather than continuing in the
previous one?

<snip>

> I can post my code if needed.

Yes, please do. I've already asked you to post a short but complete
program which demonstrates the problem...

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

P: n/a
Opa
ok, I tried to include as little code as possible. The app uses three
different assemblies as follows:

//
//Here are the the code snippets of the classes in my TSC.VXMLEngine Assembly
//In a nutshell the VXmlApplication is a singleton class
//which has one VXmlDocuments object containing 0 -n VXmlDocument objects.
Each VXmlDocument contains 0 - n VXmlDialog objects
// when a VXmlDialog is added. I am calling RaiseEventDialogAdded of the
VXmlApplication Instanace which should fire the event
//but it doesn't
using System;

namespace TSC.VXMLEngine
{
public class VXmlApplication
{

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

//declare delegates (event handlers) for event subscribers
public delegate void DocumentAddedHandler(object sender, VXmlDocument
document);
public delegate void DialogAdded(object sender, VXmlDialog dialog);
//declare event for to be handled by the delegates
public event DocumentAddedHandler OnDocumentAdded;
public event DialogAdded OnDialogAdded;

private static volatile VXmlDocuments _vxmlDocuments;

// make the default constructor protected, so that no one can directly
create it.
protected VXmlApplication()
{
if (_vxmlDocuments == null)
_vxmlDocuments = new VXmlDocuments();
}

// public property that can only get the single instance of this class.
public static VXmlApplication 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 VXmlApplication(); //create new instance of this class
}
}
}
// return instance where it was just created or already existed.
return _instance;
}
}

public VXmlDocuments Documents
{
get
{
return _vxmlDocuments;
}
}

internal void RaiseEventDocumentAdded(VXmlDocument document)
{
//if anyone has subsribed, then notify them
if (OnDocumentAdded != null)
{
OnDocumentAdded(this, document);
}
}

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

private VXmlDialogs _dialogs;
private DocumentTypes _documentType;
private VXmlApplication _ownerApplication;

private string _name;
private string _fileName;
public VXmlDocument()
{
_dialogs = new VXmlDialogs();
}
public VXmlDialogs Dialogs
{
get
{
return _dialogs;
}
}

public string Name
{
get
{
return _name;
}
set
{
_name = value;
_fileName = _name + ".vxml";
}
}
}
public class VXmlDocuments
{
public VXmlDocuments()
{
}

public int Add(VXmlDocument value)
{
VXmlApplication.Instance.RaiseEventDocumentAdded(v alue);
return base.List.Add(value as VXmlDocument);
}
}
public abstract class VXmlDialog
{
public VXmlDialog()
{
}
}
public class VXmlDialogs
{
public VXmlDialogs()
{
}

public int Add(VXmlDialog value)
{
VXmlApplication.Instance.RaiseEventDialogAdded(val ue);
return base.List.Add(value as VXmlDialog);
}
}
}


//Here is my derived classes in a different assembly
using System;
using System.IO;
using TSC.VXMLEngine;

namespace SurveyCast.Designer.Engine.Voice
{

public class VoiceEngine : VXmlEngine
{

private static volatile VoiceEngine _instance = null;
private static object syncRoot = new object();
public int iObjectID = 0;
private SurveyCast.Designer.Engine.Voice.VoiceSurveyProjec t _currentSurvey;

// 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;
}
}
public VoiceSurveyProject CreateSurveyProject()
{
//_currentSurvey = VoiceSurveyProject.Create();
_currentSurvey = new VoiceSurveyProject();
_currentSurvey.OnDialogAdded +=new
TSC.VXMLEngine.VXmlApplication.DialogAdded(_curren tSurvey_OnDialogAdded);
return _currentSurvey;
}

}
public class VoiceSurveyProject : VXmlApplication
{

private VXmlDocument _currentDocument;

//prevent direct creation
public VoiceSurveyProject()
{
}
public VXmlDocument CurrentDocument
{
get
{
return _currentDocument;
}
set
{
_currentDocument = value;
}

}

public static VoiceSurveyProject Create()
{
return new VoiceSurveyProject();
}

public void Load()
{
VXmlDocument document = new VXmlDocument();
document.Name = "Document1";
_currentDocument = document;
base.Documents.Add(document);
}
public void AddSingleChoice()
{
VoiceSingleChoice singleChoice = new VoiceSingleChoice();
_currentDocument.Dialogs.Add(singleChoice);
}
}
public class VoiceSingleChoice : VXmlDialog
{

public VoiceSingleChoice()
{
}

}
}
//finally, here is my windows application snippet
// note: the btnAddDialog_Click add a VoiceSingleChoice object which is a
VXmlDialog
using SurveyCast.Designer.Engine.Voice;
using TSC.VXMLEngine;

namespace SurveyCast.Designer.GUI.Voice
{

public class FormMain : System.Windows.Forms.Form
{

public FormMain()
{
AddEvents();
StartEngine();
}
private void StartEngine()
{
_voiceEngine = VoiceEngine.Instance;
_survey =_voiceEngine.CreateSurveyProject();
_survey.OnDialogAdded+=new
TSC.VXMLEngine.VXmlApplication.DialogAdded(_survey _OnDialogAdded);
_survey.Load();
}
private void btnAddDialog_Click(object sender, System.EventArgs e)
{
VoiceSingleChoice singleChoice = new VoiceSingleChoice();
_survey.CurrentDocument.Dialogs.Add(singleChoice);
}
private void _survey_OnDialogAdded(object sender, VXmlDialog dialog)
{

// I never get here
}
}
}

Whew! Hope you can figure out what I am trying to do.

Thanks for your help

"Jon Skeet [C# MVP]" wrote:
Opa <Op*@discussions.microsoft.com> wrote:
I wasn't sure if it would get lost in the stack.


It won't. New posts will always show up as new posts.
Yes, I am still having this problem.
I've tried for hours to figure it out. I'm still no where, can you help.


Certainly. Just post a short but complete program which demonstrates
the problem, and I'm sure it won't be too hard to fix.

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

Nov 16 '05 #7

P: n/a
Hi Opa,

That code was quite a handful :). I am going thru it...meanwhile, go thru
the following code which demostrates the inherited event I had posted about.

Copy paste and modify class location as required.

/*
*
*/
public delegate void MyHandler();
class Base
{
public event MyHandler BaseEvent;

public virtual void OnRaiseEvent()
{
if( BaseEvent!= null ) BaseEvent();
}
}

class Derv : Base
{
public event MyHandler DervEvent;

public Derv()
{
this.BaseEvent += new MyHandler(this.BaseEventHandler);
this.DervEvent += new MyHandler(this.DervEventHandler);
}

public void RaiseEvents()
{
if( DervEvent!=null ) DervEvent();
//if( BaseEvent!=null ) - This is not legal
// BaseEvent();
OnRaiseEvent(); // So raise base events like this
}

public override void OnRaiseEvent()
{
// You might want to intercept the event by
//overriding the Onxxx method like this
Console.WriteLine("Invoking base event");
base.OnRaiseEvent ();
}

private void BaseEventHandler()
{
Console.WriteLine("Handling BASE event");
}
private void DervEventHandler()
{
Console.WriteLine("Handling DERV event");
}
}

/*
*
*/
static void Main(string[] args)
{
Derv d = new Derv();
d.RaiseEvents();
}

HTH,
Rakesh Rajan
"Opa" wrote:
Hi Rekesh,

See the reply I posted to Jon with my sample code.

"Rakesh Rajan" wrote:
Hi Opa,

Please post the code.

Regards,
Rakesh Rajan

"Opa" wrote:
Hi Jon,
I wasn't sure if it would get lost in the stack. Yes, I am still having
this problem.
I've tried for hours to figure it out. I'm still no where, can you help.

Thanks
"Jon Skeet [C# MVP]" wrote:

> Opa <Op*@discussions.microsoft.com> wrote:
> > I have tried for two days to solve this problem with no luck.
>
> Any reason for posting a new thread rather than continuing in the
> previous one?
>
> <snip>
>
> > I can post my code if needed.
>
> Yes, please do. I've already asked you to post a short but complete
> program which demonstrates the problem...
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Nov 16 '05 #8

P: n/a
Opa
Ok,

I will wait for your reply.

Thanks for your help.

"Rakesh Rajan" wrote:
Hi Opa,

That code was quite a handful :). I am going thru it...meanwhile, go thru
the following code which demostrates the inherited event I had posted about.

Copy paste and modify class location as required.

/*
*
*/
public delegate void MyHandler();
class Base
{
public event MyHandler BaseEvent;

public virtual void OnRaiseEvent()
{
if( BaseEvent!= null ) BaseEvent();
}
}

class Derv : Base
{
public event MyHandler DervEvent;

public Derv()
{
this.BaseEvent += new MyHandler(this.BaseEventHandler);
this.DervEvent += new MyHandler(this.DervEventHandler);
}

public void RaiseEvents()
{
if( DervEvent!=null ) DervEvent();
//if( BaseEvent!=null ) - This is not legal
// BaseEvent();
OnRaiseEvent(); // So raise base events like this
}

public override void OnRaiseEvent()
{
// You might want to intercept the event by
//overriding the Onxxx method like this
Console.WriteLine("Invoking base event");
base.OnRaiseEvent ();
}

private void BaseEventHandler()
{
Console.WriteLine("Handling BASE event");
}
private void DervEventHandler()
{
Console.WriteLine("Handling DERV event");
}
}

/*
*
*/
static void Main(string[] args)
{
Derv d = new Derv();
d.RaiseEvents();
}

HTH,
Rakesh Rajan
"Opa" wrote:
Hi Rekesh,

See the reply I posted to Jon with my sample code.

"Rakesh Rajan" wrote:
Hi Opa,

Please post the code.

Regards,
Rakesh Rajan

"Opa" wrote:

> Hi Jon,
> I wasn't sure if it would get lost in the stack. Yes, I am still having
> this problem.
> I've tried for hours to figure it out. I'm still no where, can you help.
>
> Thanks
>
>
> "Jon Skeet [C# MVP]" wrote:
>
> > Opa <Op*@discussions.microsoft.com> wrote:
> > > I have tried for two days to solve this problem with no luck.
> >
> > Any reason for posting a new thread rather than continuing in the
> > previous one?
> >
> > <snip>
> >
> > > I can post my code if needed.
> >
> > Yes, please do. I've already asked you to post a short but complete
> > program which demonstrates the problem...
> >
> > --
> > Jon Skeet - <sk***@pobox.com>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
> >

Nov 16 '05 #9

P: n/a
Opa <Op*@discussions.microsoft.com> wrote:
ok, I tried to include as little code as possible. The app uses three
different assemblies as follows:


Unfortunately, it doesn't compile, and you haven't even provided a Main
method.

Try again, and this time please try to compile what you're going to
post (and *just* that) before posting.

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

P: n/a
Hi Opa,

It took me a looooong time to figure out what exactly you were trying to do
and I should say that even now I am not completely clear on the code. You
hadn't posted some of the code so I created a lite copy based on my limited
understanding of your design and worked with it. With respect to that, this
is what I believe your issue is:

public VoiceSurveyProject CreateSurveyProject()
{
//_currentSurvey = VoiceSurveyProject.Create();
_currentSurvey = new VoiceSurveyProject();
_currentSurvey.OnDialogAdded +=new
TSC.VXMLEngine.VXmlApplication.DialogAdded(_curren tSurvey_OnDialogAdded);
return _currentSurvey;
}

Why do you want to create a new VoiceSurveyProject instance here? Shouldn't
you be working with the singleton instance intead?
Also, the issue probably lies here - you are assigning the event handler to
this new instance, rather than the singleton instance. Assign the event
handler to the singleton instance and try.

HTH,
Rakesh Rajan

"Opa" wrote:
Ok,

I will wait for your reply.

Thanks for your help.

"Rakesh Rajan" wrote:
Hi Opa,

That code was quite a handful :). I am going thru it...meanwhile, go thru
the following code which demostrates the inherited event I had posted about.

Copy paste and modify class location as required.

/*
*
*/
public delegate void MyHandler();
class Base
{
public event MyHandler BaseEvent;

public virtual void OnRaiseEvent()
{
if( BaseEvent!= null ) BaseEvent();
}
}

class Derv : Base
{
public event MyHandler DervEvent;

public Derv()
{
this.BaseEvent += new MyHandler(this.BaseEventHandler);
this.DervEvent += new MyHandler(this.DervEventHandler);
}

public void RaiseEvents()
{
if( DervEvent!=null ) DervEvent();
//if( BaseEvent!=null ) - This is not legal
// BaseEvent();
OnRaiseEvent(); // So raise base events like this
}

public override void OnRaiseEvent()
{
// You might want to intercept the event by
//overriding the Onxxx method like this
Console.WriteLine("Invoking base event");
base.OnRaiseEvent ();
}

private void BaseEventHandler()
{
Console.WriteLine("Handling BASE event");
}
private void DervEventHandler()
{
Console.WriteLine("Handling DERV event");
}
}

/*
*
*/
static void Main(string[] args)
{
Derv d = new Derv();
d.RaiseEvents();
}

HTH,
Rakesh Rajan
"Opa" wrote:
Hi Rekesh,

See the reply I posted to Jon with my sample code.

"Rakesh Rajan" wrote:

> Hi Opa,
>
> Please post the code.
>
> Regards,
> Rakesh Rajan
>
> "Opa" wrote:
>
> > Hi Jon,
> > I wasn't sure if it would get lost in the stack. Yes, I am still having
> > this problem.
> > I've tried for hours to figure it out. I'm still no where, can you help.
> >
> > Thanks
> >
> >
> > "Jon Skeet [C# MVP]" wrote:
> >
> > > Opa <Op*@discussions.microsoft.com> wrote:
> > > > I have tried for two days to solve this problem with no luck.
> > >
> > > Any reason for posting a new thread rather than continuing in the
> > > previous one?
> > >
> > > <snip>
> > >
> > > > I can post my code if needed.
> > >
> > > Yes, please do. I've already asked you to post a short but complete
> > > program which demonstrates the problem...
> > >
> > > --
> > > Jon Skeet - <sk***@pobox.com>
> > > http://www.pobox.com/~skeet
> > > If replying to the group, please do not mail me too
> > >

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.