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

Class events

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

Nov 16 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Dave Kelly" <dk****@memco.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 16 '05 #2

P: n/a
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" <dk****@memco.co.uk> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 16 '05 #3

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

Nov 16 '05 #4

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

Nov 16 '05 #5

P: n/a
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" <dk****@memco.co.uk> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.

Nov 16 '05 #6

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

Nov 16 '05 #7

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

Nov 16 '05 #8

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

Nov 16 '05 #9

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

Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.