Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global const

Status
Not open for further replies.

carlossan

Programmer
Dec 10, 2005
18
CA
Hi,

I just discovered that C# won't let me declare a constant at the namespace level. My app has a group of classes that need to share string constants.

Any suggestion about how can I do that ?

Thanks.

Regards,

Carlos

 
Create a class that has all public const string members.
Code:
public class SharedStrings
{
   public const string Triangle = "A triangle has three sides";
   public const string Octagon = "An octagon has eight sides";

   // and so on
}
But honestly, this is the sort of thing that embedded resources were made for. Plus, they can be localized to different cultures pretty easily. Doing it this way means you can only support one culture, and adding a new culture means you have to make serious code changes, versus just creating a new resource assembly for that culture and dropping it in the correct directory.

Chip H.

____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Generally, you would add a resource to your assembly (File | Add New...), then write code to use the ResourceManager class to retrieve your strings, icons, etc.

See:
The advantage of this is when you need to localize your application for another market, all you (hopefully!) have to change are the resources.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top