Suppress "The resource name is not a valid identifier"

12,056

Solution 1

I think I can repro, although it isn't crystal in the question. Project + Properties, Resources and type a string resource name like "foo.bar" triggers this warning.

Yes, you cannot suppress this warning, it is generated by the resource designer in the IDE. When it auto-generates the code from the Properties\Resources.resx file to the Properties\Resources.Designer.cs file. Look at the .resx file and check the "name" attribute on the <data> elements. A period in the attribute value is going to trigger the warning.

A carefully crafted search-and-replace with regexp could fix that by turning the periods into underscores. That gives me personally two problems, I'd just write a little program that uses XDocument. Also check if you still need this auto-generated code, sounds like you are replacing it.

Solution 2

This will suppress all warnings for a given .cs file with

#pragma warning disable

You should also be able to right click on the warning and then click on the Show Error Help. It should give the exact warning number so that you can suppress just that warning for the entire project by going into the project properties, then the build page and entering it into the Suppress Warnings textbox.

After further research it appears that some warnings can not be suppressed. See the details on msdn about the /nowarn C# Compiler options http://msdn.microsoft.com/en-us/library/7f28x9z3.aspx

One of them is the Compiler Warning (level 1) CS2029 which refers to the "is not a valid identifier" warning you are getting. You can further confirm that it cannot be suppressed by visiting the details on the Compiler Warning (level1) CS2029 here http://msdn.microsoft.com/en-us/library/878y1894.aspx

Solution 3

A REALLY awful work-around here would be to de-couple the generated code from the Resource tool:

Let's say your resource file's name is Resources.resx

  1. Open the resource tool by double clicking the Resources.resx in the project inside Visual Studio.

  2. Set your Access modifier appropriate to the generated code scope you are using.

  3. In Windows explorer, go to the directory housing your Resources.resx and Resources.Designer.cs code. (Either manually browsing or right clicking on your project/namespace folder and selecting "Open Folder in Windows Explorer")

  4. Rename Resources.Designer.cs to Resources.cs

  5. Go back to the resource tool in Visual Studio and set the Access modifier to "No code generation"

  6. Save, close the resource tool

  7. Click the "Show All Files" icon at the top of the solution explorer window. The Resources.cs file should appear in the same folder as Resources.resx in ghosted form

  8. Right click on Resources.cs and select "Include in Project"

The warnings should now disappear. Of course, the inconvenience of this work around is that you will have to do all of these steps any time you modify the resource file.

Share:
12,056
Stu
Author by

Stu

Me? I turned my programming hobby into my profession in 1994, and I have gotten really, really good at it. The downside is that it has left me without any good hobbies. I write excellent front ends. I like to pay all the taxes to make it good, localizable, testable, task-based, consistent, and other interesting adjectives – whereas your average Enterprisy web interface just makes me twitch. But people have a way of finding out I am the only one at hand with experience in bleeding edge, obscure and weird things… so an hour later, I am suddenly working on background services, connection pool balancers, clustering, evaluating the newest Entity Framework, coding check-in policies, retrofitting mocking, porting ancient code from Fortran, diagnosing a SAN, and so on. My inner MacGyver approves. Then those things taper off and I gravitate towards more Architecty things: evaluate new version of X, or find a library to do Y, or refactor huge project to fix 2 circular references (or maybe 800). more commonly, a lot of them). There is fun in going from “finds lots of issues in code reviews” to “writes the coding standards” to “full-codebase overhaul” to “defines life cycle” and “re-did the build from scratch” and “found a way to integrate 12,000 unit tests”. Note to self: demand raise next time. Inevitably, we go from moving things to making things move faster. As much as I hate premature unfocused optimization, or throwing hardware at a problem because it almost always actually works, some times are SHOWPLAN times. I have taken a Fortune-low-number data warehouse from “cannot do daily reports because the transform takes 40 hours, we are all going to get fired” to “refreshes every hour with time to spare”. Yes, that is fun. I do not do “Big Data”. Half the people that use that term have no idea what it means, and the other half is lying their collective behinds off to puff their resume and cash in on the fad. So I’ve taken to call what I do “Lots of Data” instead. Usually it’s “Looots of Small Bits of Data”. Sooner or later I end up owning (run the systems) for 5-50TB. THAT is real. I love agile and am deft at defending it from the many that do not like it. It structurally forces ownership and that truly motivates and improves. It makes people care. Without it you get, well… Oracle. I breathe data warehouses, cubes, migrations, spatial data, partitioning, monte carlo, scale out, cloud and more. I’ve tackled – nay, spanked – the vagaries of health care, energy, and telecom. The above is not a resume, just personal observations. I hope it amused you and spoke to you. If so, you should seriously consider giving me a lot of money to make your software and your life much better. And after that, you should give me a lot of money to me a lot of money to make your software and your life much better.

Updated on June 09, 2022

Comments

  • Stu
    Stu almost 2 years

    I have a project with 5000+ resource strings in it. Almost all of them have periods in their identifier.

    We're switching over to automatically generating strongly-typed classes, and of course, because of the periods, we see a few thousand warnings

    The resource name 'blah' is not a valid identifier.

    I know it isn't, the generator changes periods to underscores, and everything is fine.

    Can I suppress the warning? It doesn't seem to have an associated number to #pragma away.

  • Stu
    Stu about 12 years
    The .cs files are automatically generated. There is no warning number.
  • Stu
    Stu about 12 years
    It's not 2029. There is no number. /W0 has no effect.
  • Stu
    Stu about 12 years
    No, we're moving from manual reflection to auto-generated classes. Just renaming won't work since we have an entire i18n toolchain that keys off these identifiers. Sigh...
  • user1703401
    user1703401 about 12 years
    That doesn't help me help you. The warning being generated by the resource designer is still my favorite explanation.
  • Stu
    Stu about 12 years
    Should've been more clear, I guess. Unless someone comes up with a workaround I'll give you the bounty anyways.
  • shashwat
    shashwat almost 11 years
    and what if I need code (.Designer.cs) and can't rename resource name at all..?? is there any other workaround..?