none
How to Programmatically Get the Name of a String Resource RRS feed

  • Question

  • I hope I'm not missing something obvious, but I'd like to be able to programatically get the name of a string resource using the strongly typed name of the string as noted in the sample (infeasible) code below.  I want to be able to do this so that I can maintain strongly-typed references to the messages while being able to store those references for later display. 

    Thanks in advance for any help you can provide.

    Steve

    // Setup string[] msgNameArray = new string[10];

    // Capture the message name. The Name property is bogus. msgNameArray[i] = Resources.MyMsg.Name;

    // Later.. string msg = Resources.ResourceManager.GetString(msgNameArray[i]);






    • Edited by Cincy Steve Tuesday, August 7, 2012 5:28 PM
    Tuesday, August 7, 2012 5:25 PM

Answers

  • Mike -

    I think we're getting closer, but still aren't there on what my question is.  My intent is to put the message strings in the Resource file at design time, so I don't need to know how to do that at runtime.  And yes, I will access them via Resources.ResourceManager.GetString(msgNameArray[i]) when it's time to display them to the user.  The question is, how can I get the message name (i.e. the name of the Resource string) as a string to store in msgNameArray?

    I woke up this morning with the thought of casting the Resource string to something that has a Name property.  But I guess I can only cast a String to an Object and Object doesn't have a Name property.

    Unless you have a way of getting the Resource string's name as a string via some Reflection method, I'm beginning to think I'm stuck having to either: (1) declare a string containing the name of the Resource string for every Resource string I want to be able to access this way; (2) collect the entire Resource string itself for later display to the user; or (3) structure the Resource string names in such a way that I can collect the messages by number (e.g. message 15 or 200) and then programmatically build the Resource string name (e.g. msgName = "Msg" + msgNumberArray[i].ToString() to yield "Msg15" or "Msg200").  I think I like the 3rd alternative best.

    Again, thanks for trying to understand and help.

    Steve

    Friday, August 10, 2012 12:37 PM

All replies

  • It's straight forward right?

    Add a Resource file to the project (I will name it MyResource.Resx). Then I will add a string resource "Name = Country, Value = XYZ" to MyResources.Resx file. Then I can access the Country as,

    String countryName = MyResource.Country;

    Please excuse me if I have misunderstood your question.

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Wednesday, August 8, 2012 6:51 AM
  • Adavesh -

    I think my question must not have been clear. 

    I know how to get the value ("XYZ") of the string named Country that's in the Resource file.  What I'm trying to get is the name of the string itself, so I can put it in a collection that I will use later to get from the Resource file the value connected with that stored name.  And I want to do this without hand coding a string with the value "Country."  Think of it like this.  In a DataGridView, I can get the name of a column via the ColumnName property (without having to hand code a string with the column's name).  I'm looking for something similar for Resource strings.   In my sample code, "MyMsg" is the same as your "Country" and the Resources.MyMsg.Name reference is meant to be what I'm looking for ("Name" being a Property of the MyMsg Resource string).

    Since there obviously isn't a "Name" Property for Resource strings, I'm looking for a way to get the same result, perhaps via the use of Reflection somehow as one example.

    I hope this is clearer.

    Thanks for the help.

    Steve



    • Edited by Cincy Steve Wednesday, August 8, 2012 7:46 PM
    Wednesday, August 8, 2012 7:43 PM
  • Hi Steve,

    Welcome to the MSDN Forum.

    Please take a look at this code:

            Dim rm As New ResourceManager("YourApplicationRootNamespace.Resources", Assembly.GetExecutingAssembly)
            Dim rs As ResourceSet = rm.GetResourceSet(CultureInfo.CurrentCulture, False, False)
    
            For Each r As DictionaryEntry In rs
                Console.WriteLine(r.Key.ToString)
            Next

    You will get all the resource names.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Krishnav Thursday, August 9, 2012 2:21 PM
    Thursday, August 9, 2012 1:30 PM
    Moderator
  • Mike -

    Thanks for the suggestion.  I can see how that will get me a string collection of ALL the Resource string names.  Is there a way to then get a string representing  a SPECIFIC Resouce string's name based on the strongly-typed name of that string? 

    Let me try a different way of explaining it what I'm seeking.  My program detects situations in its data where it wants to record one or more errors for later display to the user.  When such situations are detected, I'd like to record the NAME of the error messages in a collection so that the error display logic can subsequently access and display those messages.   And I'd like to do that using the strongly typed name of the Resource string that contains the message so that I get the benefit of the strong typing.  I could instead record the messages themselves in a collection, but collecting the message names seems like a tighter solution and I need to persist the list of messages in a database on occasion, which is better done by name in my view than by the full messages (some of which could be lengthy, though not too long).

    So, the question is, given a strongly typed Resource string name such as "Resources.MyMsg1," is there a way to get the name "MyMsg1" in a string itself without having to manually declare a string constant containing the name?

    Hope that helps.

    Steve

    Thursday, August 9, 2012 5:42 PM
  • Hi Steve,

    Thank you for clarification.

    I think I catch you now: Although we can get a resource string by this way: 

    String countryName = MyResource.Country;

    But you want to retrieve it like this way:

    string msg = Resources.ResourceManager.GetString(msgNameArray[i]);

    So now, the question is how to store the string Country into the array msgNameArray .

    Am I right?

    If so, would you like to show me how you store the message in the resource? Thank you.

    You can add it to an array when you log a message into resource. And then, save this array to resource too.

    In addition, you can try this way:

    Before the application start, such as form_load event, make a global array, and fill it by my above code. And then you can achieve this goal.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 10, 2012 2:35 AM
    Moderator
  • Mike -

    I think we're getting closer, but still aren't there on what my question is.  My intent is to put the message strings in the Resource file at design time, so I don't need to know how to do that at runtime.  And yes, I will access them via Resources.ResourceManager.GetString(msgNameArray[i]) when it's time to display them to the user.  The question is, how can I get the message name (i.e. the name of the Resource string) as a string to store in msgNameArray?

    I woke up this morning with the thought of casting the Resource string to something that has a Name property.  But I guess I can only cast a String to an Object and Object doesn't have a Name property.

    Unless you have a way of getting the Resource string's name as a string via some Reflection method, I'm beginning to think I'm stuck having to either: (1) declare a string containing the name of the Resource string for every Resource string I want to be able to access this way; (2) collect the entire Resource string itself for later display to the user; or (3) structure the Resource string names in such a way that I can collect the messages by number (e.g. message 15 or 200) and then programmatically build the Resource string name (e.g. msgName = "Msg" + msgNumberArray[i].ToString() to yield "Msg15" or "Msg200").  I think I like the 3rd alternative best.

    Again, thanks for trying to understand and help.

    Steve

    Friday, August 10, 2012 12:37 PM
  • Hi Steve,

    Thank you for sharing your solution here.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 13, 2012 6:44 AM
    Moderator