Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress "The resource name is not a valid identifier"

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.

like image 745
Stu Avatar asked Apr 04 '12 15:04

Stu


3 Answers

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.

like image 161
Hans Passant Avatar answered Nov 05 '22 06:11

Hans Passant


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

like image 45
Dan P Avatar answered Nov 05 '22 06:11

Dan P


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.

like image 25
MutantNinjaCodeMonkey Avatar answered Nov 05 '22 04:11

MutantNinjaCodeMonkey