Yet another global vars question

L

Lee

I've been programming with Delphi for the past 4 years or so and while
Delphi does allow globals, I use them very judiciously. I say that I
*do* use them because I think that in some cases they are a good choice.

Now in C# there are no global vars. MSDN and google say that I can use
static classes (err, or static methods in classes) like Type.TypeOf,
etc.

What if I have an object that contains resources for the rest of my
application such as a pre-opened database connection or maybe static
data that makes sense to keep in memory because it's used so often. Is
overriding the constructor of every object in the application the only
(best? Advised?) way of sharing an object accross the application?

Thanks for any clarity..

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
J

Jon Skeet [C# MVP]

Lee said:
I've been programming with Delphi for the past 4 years or so and while
Delphi does allow globals, I use them very judiciously. I say that I
*do* use them because I think that in some cases they are a good choice.

Now in C# there are no global vars. MSDN and google say that I can use
static classes (err, or static methods in classes) like Type.TypeOf,
etc.

What if I have an object that contains resources for the rest of my
application such as a pre-opened database connection or maybe static
data that makes sense to keep in memory because it's used so often. Is
overriding the constructor of every object in the application the only
(best? Advised?) way of sharing an object accross the application?

No, in those cases you can use public static members - preferrably
either constants or properties which can return the data (possibly
having lazily loaded them, etc).

For database connections, however, you should usually just let the
built-in connection pooling do the job for you - open the connection
and close it as early as possible, and the connection pool will keep
the *actual* connection open for you.
 
M

Michael Nemtsev

Hello lee,

For your reason pooling and cache is solutions.
Connections generally keeps in pool.
If u want some object to be in memmory - keep them in cache.
Sharing object accross application depends on this app. it's either on singleton/monostate
object, or horisonted scaled object

l> I've been programming with Delphi for the past 4 years or so and
l> while Delphi does allow globals, I use them very judiciously. I say
l> that I *do* use them because I think that in some cases they are a
l> good choice.
l>
l> Now in C# there are no global vars. MSDN and google say that I can
l> use static classes (err, or static methods in classes) like
l> Type.TypeOf, etc.
l>
l> What if I have an object that contains resources for the rest of my
l> application such as a pre-opened database connection or maybe static
l> data that makes sense to keep in memory because it's used so often.
l> Is overriding the constructor of every object in the application the
l> only (best? Advised?) way of sharing an object accross the
l> application?
l>
l> Thanks for any clarity..
l>
l> "Upon further investigation it appears that your software is missing
l> just one thing. It definitely needs more cow bell..."
l>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

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