473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

.resx files and accessing the "Name" value

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
11 13336
Frinavale
9,735 Expert Mod 8TB
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
7,872 Expert 4TB
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
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
9,735 Expert Mod 8TB
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
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
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
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
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
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
7,872 Expert 4TB
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

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

Similar topics

2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
2
by: John Davis | last post by:
I want to know what's the differences between Request.Form("Field Name") and Request.QueryString("Field Name") OR they function exactly the same, which is to return the value of the field?? ...
6
by: Java script Dude | last post by:
We just discovered another IE bug. When an html form contains an element with a name of `name` IE's internal index screws up the .name property of the containing form to point to the bad element...
4
by: Marc Elser | last post by:
Hi Everybody, Can someone please tell me how to access the form name if there's a form field named "name", for example: <form name="myform"> <input type="text" name="name" value="Marc">...
3
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their...
7
by: charliewest | last post by:
Using .Net Compact Framework, I have an ImageButton control (derived from the Button control) this is wired to a click event. The click event's receives the standard arguments (sender, e). How is...
5
by: Alex Maghen | last post by:
The editor says that when using <a name="xyz" />, the "name" element is "obsolete" but it doesn't suggest a replacement. What's wrong with <a name="xyz" /> to set a page anchor?
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
2
by: lovecreatesbea... | last post by:
Similar macros like "CONST_INT" in product code. Does it deserve this? Thank you for your time. /* a.h ***********************************************************/ #ifndef CONST_INT #define...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.