473,409 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

How to get global values

Dom
In C++ and VB, it is pretty easy to get a global constant, one that
can be used by all modules. I still have a need for this in CSharp,
but there is not easy way to get these. I can't, for example, just
"include" a file of constants.

Is it a good idea to create a class like the following:

class Parameters
{
public string FN_RegisteredServers;
public string FN_Sources;

public Parameters()
{
FN_RegisteredServers = Application.StartupPath + "\
\RegisteredServers.xml";
FN_Sources = Application.StartupPath + "\\Sources.xml";
}
}

Then, when a class needs a parameter, it does the following;

Parameters p = new Parameters ();
string FN_RegisteredServers = p.FN_RegisteredServers;

It just seems wrong that I need a new instance everytime I use it.

Dom

Aug 1 '07 #1
4 1339
Dom <do********@gmail.comwrote:
In C++ and VB, it is pretty easy to get a global constant, one that
can be used by all modules. I still have a need for this in CSharp,
but there is not easy way to get these. I can't, for example, just
"include" a file of constants.
<snip>
It just seems wrong that I need a new instance everytime I use it.
Indeed - they should be static instead, so you don't need to create a
new instance. They should also be readonly though. Use a static
constructor to set them up. Alternatively, follow the singleton
pattern:

http://pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '07 #2
On Aug 2, 4:35 am, Dom <dolivas...@gmail.comwrote:
In C++ and VB, it is pretty easy to get a global constant, one that
can be used by all modules. I still have a need for this in CSharp,
but there is not easy way to get these. I can't, for example, just
"include" a file of constants.

Is it a good idea to create a class like the following:

class Parameters
{
public string FN_RegisteredServers;
public string FN_Sources;

public Parameters()
{
FN_RegisteredServers = Application.StartupPath + "\
\RegisteredServers.xml";
FN_Sources = Application.StartupPath + "\\Sources.xml";
}
}

Then, when a class needs a parameter, it does the following;

Parameters p = new Parameters ();
string FN_RegisteredServers = p.FN_RegisteredServers;

It just seems wrong that I need a new instance everytime I use it.

Dom
just create a static class to store these static global values, a
utility class

Aug 2 '07 #3
Dom
On Aug 1, 4:45 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Dom <dolivas...@gmail.comwrote:
In C++ and VB, it is pretty easy to get a global constant, one that
can be used by all modules. I still have a need for this in CSharp,
but there is not easy way to get these. I can't, for example, just
"include" a file of constants.

<snip>
It just seems wrong that I need a new instance everytime I use it.

Indeed - they should be static instead, so you don't need to create a
new instance. They should also be readonly though. Use a static
constructor to set them up. Alternatively, follow the singleton
pattern:

http://pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks for this. Is there any practical reason why I would choose a
static class instead of a singleton?

Dom
Aug 2 '07 #4
Dom <do********@gmail.comwrote:
Thanks for this. Is there any practical reason why I would choose a
static class instead of a singleton?
If you never need anything other than constants, and don't need to use
the singleton instance for any other purpose, a static class can be
simpler. On the other hand, it can be less extensible later on - using
a singleton, you can often keep the fact that it *is* a singleton away
from quite a lot of other classes, which can make unit testing easier
etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cecil Westerhof | last post by:
With global.asa application variables can be set. Later can a script in your application change the value of those variables. But when IIS is restarted the old values are used again. Is there a way...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
6
by: Frank Swarbrick | last post by:
Interesting! I was going to ask if such a thing existed, but I was pretty much convinced they did not so I didn't ask. Looks like with version 9.5 DB2 supports global variables: "Global...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.