Kevin
Kevin

Reputation: 3623

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?

Upvotes: 9

Views: 30652

Answers (7)

Daniel Brückner
Daniel Brückner

Reputation: 59705

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.

Upvotes: 9

rotti2
rotti2

Reputation: 251

I guess that you have a resource named "MB ArchiveRestore.cs 11". Since VS 2005 the compiler (or more precisely an add-on tool) will automatically generate access classes for embedded resources. These can be used to retrieve the resources. The class property for your example is probably Properties.Resources.MB_ArchiveRestore.cs_11. These stronlgy typed resource classes also provide a convenient way for localization.

Upvotes: 0

Derick Bailey
Derick Bailey

Reputation: 72888

"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:

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:

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

Upvotes: 1

Tarka
Tarka

Reputation: 4043

I would have to guess that it's complaining about the _11 at the end, which would make it not a valid C# file, thus, not a valid resource.

Upvotes: 0

Marek
Marek

Reputation: 10402

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"]

Upvotes: 9

John Feminella
John Feminella

Reputation: 311675

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.

Upvotes: 7

Ian
Ian

Reputation: 34539

I'm not sure if it will help you fix your problem but in the interest of answering your actual question. Strongly typed means that the the variable is of a given type, as opposed to some type determined at run time. Check out Wikipedia

For example:

Int32 counter;

Means the counter variable is strongly typed, as we know it is an Int32. Other languages or use of dynamic keywords mean:

dynamic counter = ResultOfFunc()

means that counter is not strongly typed, as it is determined at run time by the result of the ResultOfFunc().

Upvotes: 0

Related Questions