Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve this "Resource name is not a valid identifier" compiler warning

I've got a bunch of Warnings in a Visual Studio 2005 project, most of them are warning me that a resource name is not a valid identifier.

example:

The resource name 'MB_ArchiveRestore.cs_11' is not a valid identifier.

MSDN online help indicates that the resource name needs to be strongly typed, and not contain spaces. What does strongly typed exactly mean?

like image 537
Kevin Avatar asked Feb 16 '10 15:02

Kevin


4 Answers

Strongly typed means that a variable, field, or property is of a specific type instead of just Object.

public class User
{
    public String FirstName { get; set; } // Strongly typed
    public Object LastName { get; set; } // Weakly typed
}

If you use strongly typed resources, code is generated with strongly typed properties for all your resources. In this case the resource name is used as the property name, hence it must be a valid C# property name. Your example MB_ArchiveRestore.cs_11 contains a dot and is in consequence not a valid property name. The code generator will replace the dot with an underscore to make the name valid and gives you the described warning to inform you about that.

like image 93
Daniel Brückner Avatar answered Nov 19 '22 07:11

Daniel Brückner


Based on the link you have posted in the question, I think that you are probably asking about strongly typed resource generation - that means that Visual Studio will generate a resources file which will allow you to access resources via typed properties, e.g.

string fileName = Resources.FileName;
bool someSetting = Resources.AllowDelete;
byte[] binaryResource = Resources.SomeFile;

as opposed to untyped resources where you have to cast the return value by yourself because it returns type System.Object instead of a specific type.

string fileName = (string)Resources["FileName"];
bool someSetting = (bool)Resources["AllowDelete"];
byte[] binaryResource = (byte[])Resources["SomeFile"]
like image 9
Marek Avatar answered Nov 19 '22 06:11

Marek


The problem occurs because . is not a valid character in identifiers.

What does strongly typed exactly mean?

Although it's not as relevant for this particular question, "strongly typed" means that an object has a definite notion of type. For example, you can't do int i = "5"; in C#, because "5" is a string and i is an integer -- their types are incompatible with each other.

This is contrast to "weakly typed" languages, where the notion of "type" is not as strong. A weakly typed language might decide that for something like i = 5; j = "6"; print (i + j);, the correct response is 11.

like image 7
John Feminella Avatar answered Nov 19 '22 08:11

John Feminella


"Strongly Typed" in this case, means that Visual Studio is trying to generate an object model for you to use, from your resource names.

For example, say you have a resource file with URLs that point to your favorite websites. your resources are something like:

  • Google: http://google.com
  • Microsoft: http://www.microsoft.com

when the resource generate is used to strongly type this, you will end up with an object model that can be called like this:

string googleUrl = Resources.Google; string msUrl = Resources.Microsoft;

when you have a period in the name of the resource, the code generator cannot use it because it would create invalid names in the Resources object. for exmaple:

  • Asp.NET: http://asp.net

this would be invalid because it would try to create a resource named Resources.Asp.NET

like image 1
Derick Bailey Avatar answered Nov 19 '22 06:11

Derick Bailey