473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.resx files and accessing the "Name" value

81 New Member
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.ERROR 1 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 13403
Frinavale
9,735 Recognized Expert Moderator Expert
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.ERROR 1 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 Recognized Expert Expert
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
nelsonbrodyk
81 New Member
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 Recognized Expert Moderator Expert
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...splitt ing on the "-" to retrieve the 2 strings.

-Frinny
Dec 15 '08 #5
nelsonbrodyk
81 New Member
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
nelsonbrodyk
81 New Member
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 Recognized Expert Moderator Expert
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
nelsonbrodyk
81 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

2
5618
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 check the script, and let me know the reason? Thanks in advance. Liang
2
17748
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?? Thanks, John
6
2301
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 object instead of containing the string name property of the form. Here is the simplified test case: <html>
4
3957
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"> <input type="button" onClick="alert(this.form.name);"> </form>
3
2688
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 "name" property (which sources from "name" HTML attribue). The problem is, that if the form contains form control named "name", it overwrites the form name property. In fact, I'm quite surprised that it's so easy to spoil any of the form object...
7
14449
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 it possible from within the click event function to get the "name" property of the control? For example: ImageButton ImageButton1 = new ImageButton(); ImageButton1.Click += new System.EventHandler(ImageButton1_Click); .... code private void...
5
1785
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
7078
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 action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
2
1462
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 CONST_INT(name, value) extern const int name #endif #ifndef CONST_DBL #define CONST_DBL(name, value) extern const double name
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.