473,394 Members | 1,640 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,394 software developers and data experts.

global const?

Hi All,

I'm new to C# so bear with me. I have a public string const that I
need to be visible within any class within my namespace so I'll have
one place to maintain the string value and have changes affect all
classes.

What is the preferred way to do this?

Thanks,
Steve
Nov 13 '07 #1
4 1923
I would look at string resources first, but you could do something
like:

public static readonly string MyConstant = "my constant";

You could implement a containing class for the above called
StringConstants and access the static constant via
StringConstants.MyConstant.

Colby

Nov 13 '07 #2
On 2007-11-13 11:05:59 -0800, Steve <ki****@harpservices.comsaid:
Hi All,

I'm new to C# so bear with me. I have a public string const that I
need to be visible within any class within my namespace so I'll have
one place to maintain the string value and have changes affect all
classes.

What is the preferred way to do this?
Well, you could create a class with a static member representing the string:

class Globals
{
private static string _strFoo;

public static string Foo
{
get { return _strFoo; }
set { _strFoo = value; }
}
}

Then you'd refer to the string as Globals.Foo.

You could make the class "static" also, if all it would contain are
static members.

Pete

Nov 13 '07 #3
Steve wrote:
I'm new to C# so bear with me. I have a public string const that I
need to be visible within any class within my namespace so I'll have
one place to maintain the string value and have changes affect all
classes.

What is the preferred way to do this?
Mark it internal inside a class in that namespace?
Chris.
Nov 13 '07 #4
Steve,

Well, if all of the classes in the namespace are in the same assembly,
then you can mark the constant as internal.

However, if your classes in the namespace are spread across separate
assemblies, then you will have to declare the constant as public, so that
outside of your declaring assembly, the constant can be seen. Of course,
that means that anyone anywhere can see the constant, not just the members
of that namespace.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <ki****@harpservices.comwrote in message
news:14********************************@4ax.com...
Hi All,

I'm new to C# so bear with me. I have a public string const that I
need to be visible within any class within my namespace so I'll have
one place to maintain the string value and have changes affect all
classes.

What is the preferred way to do this?

Thanks,
Steve

Nov 13 '07 #5

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

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
9
by: JKop | last post by:
Let's say you have a global const variable for the name of your application. Which do you think is preferrable?: A) char const g_application_name = "ChocolateCheese"; B) const char* const...
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
7
by: bob | last post by:
Stroustrup 3rd edition, 4.9.4, 2nd paragraph: "A name is called global if it is declared outside any function, class (Chapter 10), or namespace. The scope of a global name extends from the point...
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
18
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them...
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.