Resources

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greetings,

When I add a resource to the project (VS.NET) it places a .resx file. There
are many columns in the grid to input data. I just want to specify a
Name/Value pair to hold a resource of strings.

My problem is that I also have to specify a type. So I usually specify
System.String. I have no idea what I would put in there. I also have a
difficult time locating information about this topic. Most of the resources
I can find on this topic are not as straightforward as I'd like them to be.
I have "Professional C# 1st Ed.", "C# for Experience Programmers", and a few
others. MSDN isn't terribly helpful. Googling turns up too many results to
be useful but at the same time, I haven't found my answer in the first 3
pages.

I want to be able to add a Resx file. Specify a Name/Value pair. In my
code, look up the Name and be able to use the value. Sounds simple, right?
I've tried using the ResourceManager class available in most examples but it
always fails.

What must I do? I don't want to resource to using a .txt file for my
resources.


Thanks,
Shawn
 
Is it for an executable or a web application? If so you can look at using either an appname.exe.config or web.config respectively. Take a look at the System.Configuration namespace for more info

For libraries (dll's) there isn't a clean way to get name/value properties that I know of.

~harri
weblog: http://www.harrisreynolds.net/weblog
 
Hi Shawn

I had faced a similar problem a few months back - I had to put a lot of message strings and their corresponding message types in a resx and then load it, when a message is to be presented to the user. What I did was as follows

I defined all possible message types in a Messages enumeration. I also defined a class to wrap the implementation. For all these messages, the corresponsing message strings, button types, and icons are stored in the <classname>.resx resource file. When the class loads, it's resource manager member loads the resx file. I defined a static method ShowMessage in the class, which accepts a Messages enumeration value. This would take the values from the resx file and show the required messagebox.

In the resx, the 'name' column will have exactly the same name as that of the Messages enum values. Their corresponsing 'value' column will have the message to be displayed. This type would be System.String. If you need to put the buttons requires, and Icon to be show, set the value to the enum value and set the type to say System.Windows.Forms.MessageBoxButtons

To load the resource use
private static System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof <class name>))

To show the message
MessageBox.Show
resources.GetString(msgMessageType.ToString()) + ((ExtendedMessage==string.Empty)?string.Empty : ("\r\n\r\nMore info:\r\n" + ExtendedMessage))
"<title>"
(MessageBoxButtons)resources.GetObject(msgMessageType.ToString() + "Buttons")
(MessageBoxIcon)resources.GetObject(msgMessageType.ToString() + "Icon"
)

HTH
fbhca
 
I did as you said. I added an Resx file to an empty project, title it
"Strings".

Then I add the following code:

resource = new ResourceManager(typeof(Strings));
MessageBox.Show(resource.GetString("ALERT"));


In the resx data section, I added "Name" = "ALERT" "Value" = "This is an
alert" and "Type" = "System.String"

But this won't compile because of typeof(Strings) where strings is not a
member of the project. Am I missing something?


Thanks,
Shawn


fbhcah said:
Hi Shawn,

I had faced a similar problem a few months back - I had to put a lot of
message strings and their corresponding message types in a resx and then
load it, when a message is to be presented to the user. What I did was as
follows.
I defined all possible message types in a Messages enumeration. I also
defined a class to wrap the implementation. For all these messages, the
corresponsing message strings, button types, and icons are stored in the
<classname>.resx resource file. When the class loads, it's resource manager
member loads the resx file. I defined a static method ShowMessage in the
class, which accepts a Messages enumeration value. This would take the
values from the resx file and show the required messagebox.
In the resx, the 'name' column will have exactly the same name as that of
the Messages enum values. Their corresponsing 'value' column will have the
message to be displayed. This type would be System.String. If you need to
put the buttons requires, and Icon to be show, set the value to the enum
value and set the type to say System.Windows.Forms.MessageBoxButtons.
To load the resource use:
private static System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof said:
To show the message:
MessageBox.Show(
resources.GetString(msgMessageType.ToString()) +
((ExtendedMessage==string.Empty)?string.Empty : ("\r\n\r\nMore info:\r\n" +
ExtendedMessage)),
 
Back
Top