473,405 Members | 2,210 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,405 software developers and data experts.

Inheriting events from singleton class problem for two days

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
10 2198
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
9
by: Dave Kelly | last post by:
I have generated a class, which contains configuration data for my application. Unfortunately I cannot use a static implementation of the class, as it requires a constructor. I generate...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
0
by: Dennis | last post by:
I have a singleton class (a music player) then raises events. I originally dim an instance of this class in my main form using withevents and have event handlers for the singleton in this main...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
0
by: damianmatiasmax | last post by:
Helow, Good days. I am creating a class Base in VB.NET, then I use this class base to create to the class son across inherits, and Then generate the object COM this class son to be used in VB6....
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
5
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.