Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Compilation Error with Resource strings

Compilation Error with Resource strings

Scheduled Pinned Locked Moved C#
questionhelplearning
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jayart
    wrote on last edited by
    #1

    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?

    J 1 Reply Last reply
    0
    • J jayart

      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?

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • J Judah Gabriel 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

        J Offline
        J Offline
        jayart
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • J jayart

          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.

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          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

          J 1 Reply Last reply
          0
          • J Judah Gabriel 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

            J Offline
            J Offline
            jayart
            wrote on last edited by
            #5

            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; }

            J 1 Reply Last reply
            0
            • J jayart

              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; }

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups