global const?

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

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
 
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
 
Steve said:
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.
 
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
 
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.
 

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

Similar Threads


Back
Top