Add a Hashtable to Applicatoin to save Global data

R

Ryan Liu

Is that a good idea to add a Hashtable to System.Windows.Forms.Application
class? And we can put any user global data in it.

I have some class library used by a few other applications in slight
different way. This way (and ocz there are other ways) I don't have to user
different parameter to call a method or invent an other global data
repository.

Regards,


~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

Ryan Liu
Shanghai Fengpu Software Co. Ltd
Shanghai , China

http://www.PowerCATI.com Powerful CATI!
http://www.fpsoft.net.cn
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
 
P

Peter Duniho

Is that a good idea to add a Hashtable to
System.Windows.Forms.Application class? And we can put any user global
data in it.

I have some class library used by a few other applications in slight
different way. This way (and ocz there are other ways) I don't have to
user different parameter to call a method or invent an other global data
repository.

What's wrong with just making a static class for your global data? Or a
static member of some more-relevant class? (Stored in a Hashtable, or
whatever other container you find appropriate, of course).

I mean, I suppose you could use extension to add something to the
Application class, but I'm not sure what the point would be. What's so
tempting about the Application class that you want to put your data there
rather than something more specific to your own code?

Pete
 
P

Peter Duniho

[...]
I mean, I suppose you could use extension to add something to the
Application class [...]

Actually, strike that. I overlooked that you can only add an extension
method as an instance method, and with no way to get an instance of the
Application class, you can't extend it even via extension methods.

So you're out of luck anyway. The Application class is sealed and there's
no way for you to add new data members to it anyway, rendering the
question moot.

Pete
 
R

Ryan Liu

Peter Duniho said:
[...]
I mean, I suppose you could use extension to add something to the
Application class [...]

Actually, strike that. I overlooked that you can only add an extension
method as an instance method, and with no way to get an instance of the
Application class, you can't extend it even via extension methods.

So you're out of luck anyway. The Application class is sealed and there's
no way for you to add new data members to it anyway, rendering the
question moot.

Pete


Peter,

After the post, I find the System.Enviorment class is for this purpose. But
it's key and value is only string type. Now I do come out with my own static
class which use Hashtable internally.

Regards,
Ryan
 
M

Marc Gravell

After the post, I find the System.Enviorment class is for this purpose.
I realise you aren't using this, but for completeness, I suspect that
this is intended for environment variables
Now I do come out with my own static class which use Hashtable internally.
Nobody will stop you; but it doesn't seem very elegant; you will have
no assistance to help you know the expected keys or types for
settings. I'm not sure this is buying you much... personally I'd be
more inclined to have a usage-specific class with typed, named
properties (quite probably static).

A final thought; watch out for thread safety and object lifetime
(objects held globally won't be garbage collected, nor will anything
they can see [think "events"]).

Marc
 
C

christery

After the post, I find the System.Enviorment class is for this purpose.
I realise you aren't using this, but for completeness, I suspect that
this is intended for environment variables

as in what u see when u type "set" in a command prompt...

hmm...

this could get really complicated... a max len of env var at 255? no,
path can be longer now... but still... would not build hastables
there ;)

and yes, all strings, like WriteLine(Environment.CurrentDirectory);
will tell u where the os thinks u are...

//CY
 
C

christery

and yes, all strings, like WriteLine(Environment.CurrentDirectory);

oh, forgot the point of that.. take 2 programs (same prog) start in
differrent dir and they will tell u different directorys.. I dont
think its shacer or common, initalized by system defaults but can be
set by process

//CY
 
M

Marc Gravell

Yes; my point being - I wouldn't go near this for internal application
data (which is what you are talking about).

Marc
 
R

Ryan Liu

----- Original Message -----
From: "Marc Gravell" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Sunday, December 30, 2007 6:54 PM
Subject: Re: Add a Hashtable to Applicatoin to save Global data

I realise you aren't using this, but for completeness, I suspect that
this is intended for environment variables

Nobody will stop you; but it doesn't seem very elegant; you will have
no assistance to help you know the expected keys or types for
settings.

Oh, I come out another enum for keys.


I'm not sure this is buying you much... personally I'd be
more inclined to have a usage-specific class with typed, named
properties (quite probably static).

I try to write a generic class to set/get applicaton level variables. (this
class itself does not bind to the enum I mentioned above). By doing this, I
think I can avoid writing more and more classes or adding more and more
properties. And worse when some properties get oboslated.

Ryan

A final thought; watch out for thread safety and object lifetime
(objects held globally won't be garbage collected, nor will anything
they can see [think "events"]).

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top