Whats the best way to do this?

This is a discussion on Whats the best way to do this? within the C# Programming forums, part of the General Programming Boards category; Hey, Im working on quite a large program, and i would like to store all my error messages as string ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Whats the best way to do this?

    Hey,

    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

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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();
    }
    Output:
    Code:
    This is my first resource.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Great thanks, that works perfectly
    TNT
    You Can Stop Me, But You Cant Stop Us All

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21