By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

.resx files and accessing the "Name" value

P: 81
Hey All,
I want to use a resource file to store key/value pairs (resx file calls them Name/Value) for errors. I want the Key(Name) to be the error code, and the value to be the message.

My only issue is I do not know how to get a string representation of the Key/Name, as I want to display the code on screen as well.

For instance in the resx file:
Name | Value | Description
E001 | My Error | This is error 1

In Code:
Resources.ERROR1 will return me me "My Error", but I also want to get "E001" back as well. Is this possible in any way?

Thanks!
Dec 15 '08 #1
Share this Question
Share on Google+
11 Replies


Frinavale
Expert Mod 5K+
P: 9,731
In order to access the resource with the Key "E001" you have to know that "E001" is your key...

Why do you need to pull this out of the resource file when it is the information you need in order to access the resource in the first place?

Now, I'm noting that you are accessing the resource with the key "E001" when Resources.ERROR1 has occurred.

I really am not clear on what your problem is but consider instead of using "ERROR1" as your key value instead of "E001" to retrieve the text...then in another resource file use "ERROR1" as the key to retrieve the text "E001".

-Frinny
Dec 15 '08 #2

Plater
Expert 5K+
P: 7,872
You could mimic what the resource manager does.
Here's a snippit from the designer code:
Expand|Select|Wrap|Line Numbers
  1. internal static System.Drawing.Icon StartIcon 
  2. {
  3.    get 
  4.    {
  5.       object obj = ResourceManager.GetObject("StartIcon", resourceCulture);
  6.       return ((System.Drawing.Icon)(obj));
  7.    }
  8. }
  9.  
Dec 15 '08 #3

P: 81
Yeah, here is my challenge.

I have a resource file with error messages.
E001 - Error 1
E002 - Error 2
etc, etc

Now when I go to display the error, I want to display the error code in a message pop up title bar, and the message in the message box itself.

So I know I want to display E001, so I would simply say Resources.E001 and that gives me "Error 1".

Now when I want to put the error code E001 in the title, I don't want to hardcode that, I want to just translate the key/Name. Does that makes sense?

So in a MessageBox I want:
Expand|Select|Wrap|Line Numbers
  1. messageBox.show(Resources.E001, Resources.E001.ToString()) //where the first param is the text, and the second param is the title displaying the error code.
  2.  
instead of doing:
Expand|Select|Wrap|Line Numbers
  1. messageBox.show(Resources.E001, "E001");
  2.  
Does that help?
Dec 15 '08 #4

Frinavale
Expert Mod 5K+
P: 9,731
Since everything is in the String that you are retrieveing from the resource file, all you have to do is parse the string to your liking.....

Use the SubString() method to retrieve the "E001" and the "Error 1" form the String that you are fetching from the resource.

Or you could use the Split() method...splitting on the "-" to retrieve the 2 strings.

-Frinny
Dec 15 '08 #5

P: 81
Yes, I was thinking that, I was just curious if there was another method to do it based on the "enum" that C# generates.
Dec 15 '08 #6

P: 81
Out of curiousity, is there a better way then resx files to do what I am trying to do? I rolled my own implementation that does it for me, but it would be nice to use the resx files if possible.
Dec 15 '08 #7

Frinavale
Expert Mod 5K+
P: 9,731
Resource files are great :)

From where and how are you getting your error?

Do you have an Enum of all the possible errors?
Dec 15 '08 #8

P: 81
Yeah, basically, I want to have a resource file that lists all of my error codes and the corresponding messages. This way they can be translated, and they act like enum's almost when you access them from code. The only issue is that I need to separate the code (which is the Name/Key) from the message. That's why my own home-rolled implementation works well. But i'd love to be able to use resx file provided I can somehow say "Give me the string representation of the Key/Name E001", or whatever the error code might be.

It would be awesome to do Resources.E001.GetName(); haha

Then when I want to throw an error, I can just say, get the error from the resource file, and the from the resource file I can get the string representation of the key, as well as the message.
Dec 15 '08 #9

Frinavale
Expert Mod 5K+
P: 9,731
I've done something similar in the past.

I had an Enum containing a bunch of error codes.
I took all of the Enum Keys, added them as Keys to my Resx file, and supplied user friend messages as the values.

The code I called returned an Enum value indicating if my function-call executed successfully...if not I was able retrieve the user friendly error message from the Resx resource based on the Key of the Enum value the function returned.

There may be a better way around this because I was using an unmanaged DLL that didn't throw errors. Therefore, I had to implement my solution in this way since the unmanaged code returned Enum values instead of throwing errors.

Check out the enum class for more information on what enum methods are available to you.

-Frinny
Dec 15 '08 #10

P: 81
Yeah, this is sort of how I rolled my own implementation. Except enum's in C# are a little stupid, so I made my own "enum" style class that reads from a properties file to get the key/value pairs. That way I can just translate the properties file and it's good to go.

Thanks for the thoughts!
Dec 15 '08 #11

Plater
Expert 5K+
P: 7,872
Well if you wanted to get nasty, I think you could use Reflection to create(/update) an Enum at run time and populate them from elsewhere, but that seems a waste.

And what does: Resources.E001.ToString() actually produce?

As a side note with Reflection you can get a list of members of an object (so on the Resources object it would give you "E001","E002" and etc) but that could be more work then you want anyway
Dec 15 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.