Retrieving a resource name/value.

  • Thread starter Thread starter mehdi_mousavi
  • Start date Start date
M

mehdi_mousavi

Hi folks,
Consider a string that's defined under VS2005 resource editor as
myField with the value of myValue. To access the value, I could easily
use the Properties.Resources class, for example:

global::myNameSpace.Properties.Resources.myField

However, I would like to retreive it's string representation of the
field's name, i.e., the "myField". How am I supposed to do that?

The second question is that is there anyway to keep a "name/value"
collection within a resource file?

Any help would be highly appreciated,

Cheers,
Mehdi
 
Hi Mehdi,
global::myNameSpace.Properties.Resources.myField
However, I would like to retreive it's string representation of the
field's name, i.e., the "myField". How am I supposed to do that?

Hard-coding the string into your program might be your best bet if you only
want a limited number of strings. After all, the string "myField" is a
constant that you define at design-time.

I don't see how you could possibly retrieve the key name at runtime without
using the key name to find that resource item, but if you want to retrieve
all of the key names in your Resources object try the following code:

foreach (DictionaryEntry resourceEntry in
Properties.Resources.ResourceManager.GetResourceSet(
System.Globalization.CultureInfo.CurrentUICulture, true, true))
{
string keyName = resourceEntry.Key;
}
The second question is that is there anyway to keep a "name/value"
collection within a resource file?

I can think of a few options:

1. Serialize a Dictionary, Base64 encode it, then hard-code the value into
the resource grid.
(To read it you must unencode it and deserialize it back into a
Dictionary)
2. Use a delimited format like key=value[,key=value...] and parse the
resource into a Dictionary at runtime.
3. For option #2 you could use = and & as delimiters and encode each key and
value as if they were urls.
4. Use an xml file instead of an embedded resource.
5. Use a database.

- Dave Sexton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top