I like the XML idea, but look also at ResourceManager and GetString.
This may be what you had in mind in the original post. I would just
run the GetString() function as you need it instead of putting it in a
static constructor. Especially if it's only needed for exceptions
(which hopefully aren't occurring very often). Note that GetString is
overloaded to handle diferent CultureInfo's.
# String Table text file
Message1 = This was an error.
Message2 = This was an exceptional error.
then add the file to the assembly as a resource (see online help for
more info). The following lines of code will read it out (assuming
the resource is named StringTable):
ResourceManager stringResources = new ResourceManager("StringTable",
Assembly.GetExecutingAssembly());
string msg = stringResources.GetString("Message1");
-mike
"codewriter" <co**************@yahoo.com> wrote in message
news:0O********************@news20.bellglobal.com. ..
Another approach, that I think is a bit more flexible, is to use an
XML file and store you error messages in it. In this case, you will be able
to change your messages without using resources by simply altering your XML
file though a notepad or other editor. Also, you can create several XML
files for each culture, you may check in your code which culture it is and
name XML file accordingly. In this case you will not need to recompile your
code, but load an XML file on the fly.
"Wavemaker" <ja**********@NOSPAMhotmail.com> wrote in message
news:SFhZa.108546$uu5.15570@sccrnsc04... I'm writing a class whose methods can throw exceptions under
certain circumstances. These exceptions will need string messages. I was
thinking about placing these messages in a resource instead of
hard coding them into the class itself.
The question I'm pondering is where would be the best place to
load these string resources? The string messages will be the same for
every instance of the class, so I was thinking about loading the
resource in a static constructor, retrieving the messages, and
storing them in class fields. This prevents the resources from
being repeatedly loaded each time a new instance of the class is
instantiated.
Are there any disadvantages to this approach? The only problem I
can think of is dealing with a situation in which the culture is
changed at some point during runtime. The resources would need to be
reloaded at that point. Admittedly, at this point, I am only
supporting one culture, so this may not be an issue, but this
could change in the future.