PC Review


Reply
Thread Tools Rate Thread

Class events

 
 
Dave Kelly
Guest
Posts: n/a
 
      11th Jan 2005
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
to reload the underlying xml file whenever any of the instances change
the properties.

Is it possible for a class instance to generate an event so that all
other instances will receive the event?

Regards,

Dave

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      11th Jan 2005
Dave,

You might want to use a singleton pattern here. Instead of creating new
instances, have one instance, and then have all of your classes hook up
event handlers to that instance.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Dave Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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
> to reload the underlying xml file whenever any of the instances change
> the properties.
>
> Is it possible for a class instance to generate an event so that all
> other instances will receive the event?
>
> Regards,
>
> Dave
>



 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      11th Jan 2005
to add to Nicolas' answer:

If you use a singleton, you don't need to worry about a change in one
instance not being reflected in another, since everyone is using the same
instance.

See http://www.yoda.arachsys.com/csharp/singleton.html

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Dave Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> to reload the underlying xml file whenever any of the instances change
> the properties.
>
> Is it possible for a class instance to generate an event so that all
> other instances will receive the event?
>
> Regards,
>
> Dave
>



 
Reply With Quote
 
Dave Kelly
Guest
Posts: n/a
 
      12th Jan 2005
Thanks for the replies.

I originally started with a Singleton pattern, but it wasn't working
out.

Within my class I have properties, which are used in a property grid, a
Hastable and an XMLSerializer to store the config data.

I have managed to satisfy all the requirements, since for example the
XMLSerializer requires a public constructor of the base class.

 
Reply With Quote
 
Dave Kelly
Guest
Posts: n/a
 
      12th Jan 2005
What I meant to say above is I haven't managed to implement the
Singleton pattern because of conflicting requirements with the members
in my class.

This is why I was looking at events to try and get around the problem.
Regards.

 
Reply With Quote
 
Nick Malik [Microsoft]
Guest
Posts: n/a
 
      12th Jan 2005
Are you still looking for a solution to a problem?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Dave Kelly" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> What I meant to say above is I haven't managed to implement the
> Singleton pattern because of conflicting requirements with the members
> in my class.
>
> This is why I was looking at events to try and get around the problem.
> Regards.
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      12th Jan 2005
Then what you want is a normal (non-singleton) class that has some
static events. Client code subscribes to the static events. Whenever
the config information is reloaded, the class (not an instance) raises
an event that all clients receive.

 
Reply With Quote
 
Dave Kelly
Guest
Posts: n/a
 
      12th Jan 2005
I am still trying to find a solution.

The singleton pattern didn't work, the PropertyGrid which used my
class requires an instance to show the properties, using the instance
of the static class doesn't work.

I originally tried using static events as well, but had trouble getting
this to work as well, which prompted the message in the first place.

I have got the class working at the moment by setting a static dirty
flag in the class, then each instance can check this flag and reload
the data if another instance has modified it. However as the class
grows, and is used in more places, this becomes unwieldy.

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      12th Jan 2005
I don't understand why a singleton won't work for you. In particular, I
don't understand your comment that you "tried using an instance of the
static class" and it didn't work.

If you can create an instance then it's not a static class. If it's a
static class then you can't create an instance. A singleton _is_ an
instance: it's just that there's only ever one instance.

The only time you can't use a singleton is if you need multiple
instances that contain different data, or you are using some class or
control that expects to be able to clone / copy the instance you give
it (needs to call a constructor).

As well, what was it about static events that caused you problems? Even
if you can't use singletons, static events would get the job done.

 
Reply With Quote
 
Dave Kelly
Guest
Posts: n/a
 
      13th Jan 2005
Thanks for the reply.

For the singleton, the instance I was referring to was 'the' instance
of the class. The propertygrid does not show any properties using this
method.

I managed to figure out how to get the events working to perform the
handler in all instances, whenever one instance raises the event.

I still have yet to get it working, at the moment the load is a
XMLSerializer, which is a static function. I hopefully can get this
part working now, and solve all of my problems.

Thanks for the help.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Events not firing Geoff Microsoft Excel Programming 4 4th Apr 2008 08:32 PM
Use an instance of a class across events? Jesper F Microsoft Access VBA Modules 9 18th Jan 2008 11:07 PM
Can a Derived Class Subscribe to Base Class' Events? Jerry Nixon Microsoft C# .NET 7 7th Oct 2005 01:35 PM
Class Events Gareth Microsoft Excel Programming 16 5th Oct 2005 05:25 PM
Events or Class Overrides ? Lee Gillie Microsoft VB .NET 1 26th Jan 2004 09:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:17 AM.