Reputation: 13608
Im creating an Windows Phone app and I find myself writing the same MessageBox.Show("Same error message")
multiple times. For instance
"Could not connect to server"
This happens when the user do not have internet access.
Is there somewhere I can put it so that I write the text once and fetch the same text all over the place?
I could write a static class, but maybe there is a file for this?
Upvotes: 2
Views: 136
Reputation: 726849
Is there somewhere I can put it so that I write the text once and fetch the same text all over the place?
Yes, there is a special kind of file specifically for this, called strings.resx
. It lets you write
MessageBox.Show(strings.ServerNotFound);
instead of
MessageBox.Show("Server not found");
The added benefit (in fact, the intended purpose) of using strings.resx
is that your application becomes easily localizable (see answer to this question): adding proper translations and setting the current locale is all it would take to change all strings that your application displays to users with their proper local translations.
Upvotes: 3
Reputation: 5557
You can create a static variable in the App.xaml.cs page in the App class, so that you can access it all over the application.
Upvotes: 1
Reputation: 14972
If you want it to be multi-lingual in the end I'd go for the Resource.resx file.
If not, you can go for all kinds of solutions:
Like:
class MyClass
{
private static string MyString = "blah";
// other meaningful stuff
}
Or:
public class MyStaticStrings
{
public static string MyString = "blah1";
public static string AnotherString = "blah2";
}
Upvotes: 2