473,387 Members | 1,504 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.

Static constructors and resources

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.
Nov 15 '05 #1
3 2211
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.

Nov 15 '05 #2
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.


Nov 15 '05 #3

First of all, thanks to you and codewriter for your reply.

"Michael Mayer" wrote:
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).


I will investigate the XML idea, but the ResourceManager route is
the way I was thinking about doing it. I was just wondering how
intensive it is to load the resource and then extract the string
message. Since it's, as you've pointed out, only for use with
exceptions, which shouldn't occur very often, maybe this shouldn't
be of any concern to me.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mdc | last post by:
Hi, Is there any way to implement an interface as a static method in C#? If not, is this a bug? Micheal.
6
by: Dolphin White | last post by:
For example, I allocate some unmanaged resources in the static constructors, then how can I properly release the resource before the application exit? thx!
9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
6
by: ~~~ .NET Ed ~~~ | last post by:
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
0
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
3
by: Adam | last post by:
What happens if one thread is executing a static constructor and another thread starts. Does the second thread block until the first is done in the static constructor? I want to make sure all my...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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.