jpoh jpoh - 4 months ago 29
C# Question

Should we store format strings in resources?

For the project that I'm currently on, I have to deliver specially formatted strings to a 3rd party service for processing. And so I'm building up the strings like so:

string someString = string.Format("{0}{1}{2}: Some message. Some percentage: {3}%", token1, token2, token3, number);

Rather then hardcode the string, I was thinking of moving it into the project resources:

string someString = string.Format(Properties.Resources.SomeString, token1, token2, token3, number);

The second option is in my opinion, not as readable as the first one i.e. the person reading the code would have to pull up the string resources to work out what the final result should look like.

How do I get around this? Is the hardcoded format string a necessary evil in this case?


I do think this is a necessary evil, one I've used frequently. Something smelly that I do, is:

// "{0}{1}{2}: Some message. Some percentage: {3}%"
string someString = string.Format(Properties.Resources.SomeString
                                  ,token1, token2, token3, number); least until the code is stable enough that I might be embarrassed having that seen by others.