Class events

  • Thread starter Thread starter Dave Kelly
  • Start date Start date
D

Dave Kelly

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
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
Back
Top