Department of InformatiX
Microsoft .NET Micro Framework Tools & Resources

Custom resource identifier

If you need to load resources dynamically, you might like to change the resource ID generated for your resource. You can do that using this easy trick (not so easy to find one, though):

Put a semicolon and desired resource id (can be negative as well) after your resource name. The resource designer will be a little bit nervous, but you can compile and run the application without any problems. For example:

Resource Designer Screenshot

Sample usage:

Debug.Print(Resources.GetString((Resources.StringResources)DateTime.Now.DayOfWeek));

Remember this is an undocumented behaviour (I guess because of the warnings) and could be changed in future.

 

The following trick unfortunately no longer works in 3.0 version.

Custom resource enumeration

The next tip is about enumerations. If you start couple of resource names with the same prefix and use a dot, special enumeration will be generated for you, for example:

MyFruitEnum.Apple
MyFruitEnum.Banana
MyFruitEnum.Orange

will generate:

[System.SerializableAttribute()] internal enum MyFruitEnum : short { Banana = 16540, Apple = 27001, Orange = 31269, } internal class Resources { ... internal static string GetString(MyFruitEnum id) { return ((string)(Microsoft.SPOT.ResourceUtility.GetObject(ResourceManager, id))); } }

Debug.Print(Resources.GetString(MyFruitEnum.Apple));

Yet still, the designer will not like you either. You can even combine the previous trick with this one, having the custom enum with your predefined enum values (use MyFruitEnum.Banana;4 etc.).

Want more?

Okay, what would you say it could happen when we keep adding more dots? The answer is: more than nothing, yet still nothing useful. In fact, by using two dots, you put the type in another namespace (eg. MyNamespace.MyFruitEnum.Apple etc.):

namespace MyNamespace
{
[System.SerializableAttribute()] internal enum MyFruitEnum : short { Apple = -4859, Orange = -3030, Banana = 21283, } }

Why not useful? Let's check the accessing method as well...

namespace MFConsoleApplication1 { ... internal class Resources { ... internal static string GetString(MyFruitEnum id) { return ((string)(Microsoft.SPOT.ResourceUtility.GetObject(ResourceManager, id))); } } }

This results in a compilation error, since the MyFruitEnum does not belong to the same namespace. I think this is a bug in the code generation.

Three and more dots results are left as an excercise to the reader.

Comments
Sign in using Live ID to be able to post comments.