For this specific need, I have two const strings containing parameters, like this:
private const string myString = "The operation #{0} has been completed successfully in {1} seconds";
This type of constant is of course used with String.Format() later on, in order to generate feedback messages.
Now I am facing a specific case, where I have those two constants. My example is not the real case, for privacy purposes, so it doesn't look very useful or even logical, but I assure you the real thing makes sense in context. I just couldn't get a good idea for a fake case with the same problem:
private const string shortFormat = "operation {0} out of {1}";
private const string longFormat = "During step one, an error was encountered on operation {0} out of {1}, and during step two, an error was encountered on operation {2} out of {3}";
You can see an obvious redundancy here. One of the constants is used twice Inside the second one for literally the same purpose, just in a bigger picture.
If it was used only once, I could re-use it like this:
private const string shortFormat = "operation {0} out of {1}";
private const string longFormat = "During step one, an error was encountered on " + shortFormat + " so the process has not completed step two.";
This would look much better. But I don't know how to use it twice like in the first example. If I simply insert shrotFormat twice, then I will duplicate parameters 0 and 1, so the string at runtime will look like this:
"During step one, an error was encountered on operation {0} out of {1}, and during step two, an error was encountered on operation {0} out of {1}"
Which obviously will not work with 4 different parameters in string.Format()
How can I make this not repeat itself, while keeping all four parameter numbers different and useable with 4 values?
longFormat...Where did this runtime string come from? And why can't you just callString.Formaton each use of theshortFormatconstant?private const string runtimeText = "During step one, an error was encountered on " + string.Format(shortFormat, stepOneErrorOperation, stepOneTotalOperations) + ", and during step two, an error was encountered on " + string.Format(shortFormat, stepTwoErrorOperation, stepTwoTotalOperations) + "."readonlyinstead and do the initialization in the constructor. My point still stands.