![]() |
| | #1 |
| Registered User Join Date: Aug 2001 Location: UK
Posts: 339
| Whats the best way to do this? Im working on quite a large program, and i would like to store all my error messages as string variables inside a dedicated .cs file so they are easy to update, baring in mind these need to be accessible over the entire project, whats the best way of doing it? I realise global variables are against the idea of OOP, but there must be a suitable method for doing this I assume they need to be inside the same namespace, and also inside a class. Could some give me an example .cs file that i can include, Also can this be done so i could use the strings without making a new instance of the class everytime i need to use one. Thanks for any tips. Jack
__________________ TNT ![]() You Can Stop Me, But You Cant Stop Us All |
| (TNT) is offline | |
| | #2 |
| Anti-Poster Join Date: Feb 2002
Posts: 1,241
| In VS 2005, you can create a resources file for all of your strings. These strings are accessible to everything inside the assembly because the autogenerated class defines several internal static methods to access the strings you've added to the file. You can create this file by right-clicking on the project, clicking on Add > New Item..., and clicking on the Resources File in the dialog. Name your resources file well, because that will be the name of the class that you use to access your resources. After the file is added, you can double-click on it and add a few different kinds resources (strings being one of them). To add a string, give the string a name, then put the string in the value part. To access the string, use the class name and the string name. For example, the following image shows my sample resource file named Resources: http://fluff.thejefffiles.com/images/resources.jpg Here's the code in my program: Code: static void Main(string[] args)
{
Console.WriteLine(Resources.String1);
Console.ReadLine();
}
Code: This is my first resource.
__________________ Rule #1: Every rule has exceptions |
| pianorain is offline | |
| | #3 |
| Registered User Join Date: Aug 2001 Location: UK
Posts: 339
| Great thanks, that works perfectly
__________________ TNT ![]() You Can Stop Me, But You Cant Stop Us All |
| (TNT) is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|