473,666 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2223
Opa <Op*@discussion s.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.co m>
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*@discussion s.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Opa <Op*@discussion s.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.co m>
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*@discussion s.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.co m>
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*@discussion s.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.co m>
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 RaiseEventDialo gAdded 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 DocumentAddedHa ndler(object sender, VXmlDocument
document);
public delegate void DialogAdded(obj ect sender, VXmlDialog dialog);
//declare event for to be handled by the delegates
public event DocumentAddedHa ndler 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 RaiseEventDocum entAdded(VXmlDo cument document)
{
//if anyone has subsribed, then notify them
if (OnDocumentAdde d != null)
{
OnDocumentAdded (this, document);
}
}

internal void RaiseEventDialo gAdded(VXmlDial og dialog)
{
//if anyone has subsribed, then notify them
if (OnDialogAdded != null)
{
OnDialogAdded(t his, dialog);
}
}
}
public class VXmlDocument
{

private VXmlDialogs _dialogs;
private DocumentTypes _documentType;
private VXmlApplication _ownerApplicati on;

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(VXmlDocumen t value)
{
VXmlApplication .Instance.Raise EventDocumentAd ded(value);
return base.List.Add(v alue as VXmlDocument);
}
}
public abstract class VXmlDialog
{
public VXmlDialog()
{
}
}
public class VXmlDialogs
{
public VXmlDialogs()
{
}

public int Add(VXmlDialog value)
{
VXmlApplication .Instance.Raise EventDialogAdde d(value);
return base.List.Add(v alue as VXmlDialog);
}
}
}


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

namespace SurveyCast.Desi gner.Engine.Voi ce
{

public class VoiceEngine : VXmlEngine
{

private static volatile VoiceEngine _instance = null;
private static object syncRoot = new object();
public int iObjectID = 0;
private SurveyCast.Desi gner.Engine.Voi ce.VoiceSurveyP roject _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 VoiceSurveyProj ect CreateSurveyPro ject()
{
//_currentSurvey = VoiceSurveyProj ect.Create();
_currentSurvey = new VoiceSurveyProj ect();
_currentSurvey. OnDialogAdded +=new
TSC.VXMLEngine. VXmlApplication .DialogAdded(_c urrentSurvey_On DialogAdded);
return _currentSurvey;
}

}
public class VoiceSurveyProj ect : VXmlApplication
{

private VXmlDocument _currentDocumen t;

//prevent direct creation
public VoiceSurveyProj ect()
{
}
public VXmlDocument CurrentDocument
{
get
{
return _currentDocumen t;
}
set
{
_currentDocumen t = value;
}

}

public static VoiceSurveyProj ect Create()
{
return new VoiceSurveyProj ect();
}

public void Load()
{
VXmlDocument document = new VXmlDocument();
document.Name = "Document1" ;
_currentDocumen t = document;
base.Documents. Add(document);
}
public void AddSingleChoice ()
{
VoiceSingleChoi ce singleChoice = new VoiceSingleChoi ce();
_currentDocumen t.Dialogs.Add(s ingleChoice);
}
}
public class VoiceSingleChoi ce : VXmlDialog
{

public VoiceSingleChoi ce()
{
}

}
}
//finally, here is my windows application snippet
// note: the btnAddDialog_Cl ick add a VoiceSingleChoi ce object which is a
VXmlDialog
using SurveyCast.Desi gner.Engine.Voi ce;
using TSC.VXMLEngine;

namespace SurveyCast.Desi gner.GUI.Voice
{

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

public FormMain()
{
AddEvents();
StartEngine();
}
private void StartEngine()
{
_voiceEngine = VoiceEngine.Ins tance;
_survey =_voiceEngine.C reateSurveyProj ect();
_survey.OnDialo gAdded+=new
TSC.VXMLEngine. VXmlApplication .DialogAdded(_s urvey_OnDialogA dded);
_survey.Load();
}
private void btnAddDialog_Cl ick(object sender, System.EventArg s e)
{
VoiceSingleChoi ce singleChoice = new VoiceSingleChoi ce();
_survey.Current Document.Dialog s.Add(singleCho ice);
}
private void _survey_OnDialo gAdded(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*@discussion s.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.co m>
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. BaseEventHandle r);
this.DervEvent += new MyHandler(this. DervEventHandle r);
}

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.WriteLi ne("Invoking base event");
base.OnRaiseEve nt ();
}

private void BaseEventHandle r()
{
Console.WriteLi ne("Handling BASE event");
}
private void DervEventHandle r()
{
Console.WriteLi ne("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*@discussion s.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.co m>
> 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. BaseEventHandle r);
this.DervEvent += new MyHandler(this. DervEventHandle r);
}

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.WriteLi ne("Invoking base event");
base.OnRaiseEve nt ();
}

private void BaseEventHandle r()
{
Console.WriteLi ne("Handling BASE event");
}
private void DervEventHandle r()
{
Console.WriteLi ne("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*@discussion s.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.co m>
> > http://www.pobox.com/~skeet
> > If replying to the group, please do not mail me too
> >

Nov 16 '05 #9
Opa <Op*@discussion s.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

11
2160
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 methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
9
1329
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 instances of this class in several other classes (wherever I need to retrieve the config details). I would like any of the classes to generate an event, for all instances
13
3053
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 code looks like this: public sealed class Reference { private static readonly Reference instance = new Reference(); // Make the default constructor private, so that nothing can directly create it.
13
1940
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 indicates that the forms timer does not operate on its own thread and will not cause a query to be sent to use the single SqlConnection while another is being executed from the handling of other events. Is that right? I just want to be 100% sure...
0
958
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 class. Then in another form that I show on top of the original form, I get an instance of the singleton and also have event handlers in this form for the singleton's events. I noted that when the singleton class raises an event, it gets routed to...
3
2324
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 through an external file in which case we can use reflection to create objects of that type at run time. Case 1: The methods of those types can be invoked through reflection. Case 2: The caller can publish certain events. The types (objects)...
0
1219
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. The problem is that I do not receive the events of the class base from VB6. I show the code ---------------------------------------------------------------------------------------------------------------- Class Base - Name: A
24
2942
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 vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
5
2767
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 : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing reference, so is it puting in resource leaks? Or is this being cleared somewhere that i am not seeing?
0
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8783
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.