Compilation Error with Resource strings
-
class MyClass { private const string MY_NAME = Resources.MyResourceName; }
This gives a compilation error. The property or indexer 'App.Properties.Resources.MyResourceName' cannot be used in this context because it lacks the get accessor. How can I solve this? -
class MyClass { private const string MY_NAME = Resources.MyResourceName; }
This gives a compilation error. The property or indexer 'App.Properties.Resources.MyResourceName' cannot be used in this context because it lacks the get accessor. How can I solve this?That sounds like an designer error. Go back into your resources and make a change, then save it. It should generate a get accessor. Note that you won't be able to store it in a constant field, however. You could store it in a readonly field if you need to: private readonly string MY_NAME = Resources.MyResourceName; Also note that MY_NAME isn't following standard .NET naming conventions. Run the free Microsoft tool FxCop on your assembly, it will tell you what you're doing wrong.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Back From Vacation The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
That sounds like an designer error. Go back into your resources and make a change, then save it. It should generate a get accessor. Note that you won't be able to store it in a constant field, however. You could store it in a readonly field if you need to: private readonly string MY_NAME = Resources.MyResourceName; Also note that MY_NAME isn't following standard .NET naming conventions. Run the free Microsoft tool FxCop on your assembly, it will tell you what you're doing wrong.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Back From Vacation The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Thanks. Actually I need to use the string in a switch statement as one of the case.
class MyClass { private const string MY_NAME = Resources.MyResourceName; // gives error void Myfunction(string str) { switch(str) { case MY_NAME: // expects a contant here break; } } }
About the naming convention, this code is just sample code. -
Thanks. Actually I need to use the string in a switch statement as one of the case.
class MyClass { private const string MY_NAME = Resources.MyResourceName; // gives error void Myfunction(string str) { switch(str) { case MY_NAME: // expects a contant here break; } } }
About the naming convention, this code is just sample code.You can switch on regular strings:
string s = "foo"; switch (s) { case "foo": MessageBox.Show("it was foo"); break; }
p.s. To preserve formatting, use <pre> tags around your code when posting on this forum.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Back From Vacation The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
You can switch on regular strings:
string s = "foo"; switch (s) { case "foo": MessageBox.Show("it was foo"); break; }
p.s. To preserve formatting, use <pre> tags around your code when posting on this forum.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Back From Vacation The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
I think you are not getting the point. I want to eliminate hard coded strings hence I have stored all the hard coded strings like "foo" in Application Resources. Now I want to use these resource strings in my application. I have no problem using the resource string except in switch statements. where the case requires a constant value. This is the place where I am finding hard to replace
string s = "foo"; switch (s) { case "foo": // want to replace this hard coded string !!! MessageBox.Show("it was foo"); break; }
-
I think you are not getting the point. I want to eliminate hard coded strings hence I have stored all the hard coded strings like "foo" in Application Resources. Now I want to use these resource strings in my application. I have no problem using the resource string except in switch statements. where the case requires a constant value. This is the place where I am finding hard to replace
string s = "foo"; switch (s) { case "foo": // want to replace this hard coded string !!! MessageBox.Show("it was foo"); break; }
Ah, gotcha. I don't think that will work, because the string in the resources isn't constant. Instead of doing a switch, you should just use if/else, using string.Equals(first, second, comparisonOptionsAndCulture)
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Back From Vacation The apostle Paul, modernly speaking: Epistles of Paul Judah Himango