How to enumerate resources

Hi there
Can anybody give me a clue on how to find a resource by its key programmatically?

The problem is the WCF service returns the error message to the Android client as a key (string or number, whatever).

My task is to find that string in String resources, localize it and display it. The class Resources.String must be instantiated to use its GetType for Reflection, but it is no instantiable.

Any clue?

Thanks.

Posts

  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    Resource IDs are constant ints generated by the Android/Xamarin compiler.

    So I think you'll have to have a manually maintained lookup table in code, mapping from the returned string or int to the resource IDs.

    Something like:

    using System.Collections;
    //...
    // Initialise the lookup table
    ErrorResources = new Hashtable ();
    ErrorResources.Add ("INVALID_LOGIN", Resource.String.invalidLoginErrorMessage);
    //...
    
    
    string TextToDisplay;
    int ErrorCode;
    
    // Retrieve the ErrorCode from the service somehow
    
    // Use the returned error code to look up the resource id and get the
    // resource text
    
    TextToDisplay = Resources.GetString ((int)ErrorResources[ErrorCode]);
    

    This is a very crude implementation which I've typed from memory (so no guarantees of its ability to compile). Others will no doubt come up with more elegant solutions!

  • NikolayRenziglovNikolayRenziglov CAMember ✭✭

    Ok, Resources.GetString is the thing I need.

    Thanks.

Sign In or Register to comment.