473,585 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2220
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**********@N OSPAMhotmail.co m> wrote in message
news:SFhZa.1085 46$uu5.15570@sc crnsc04...
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 ("StringTabl e",
Assembly.GetExe cutingAssembly( ));
string msg = stringResources .GetString("Mes sage1");

-mike

"codewriter " <co************ **@yahoo.com> wrote in message
news:0O******** ************@ne ws20.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**********@N OSPAMhotmail.co m> wrote in message
news:SFhZa.1085 46$uu5.15570@sc crnsc04...
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
17187
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
6589
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
2424
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 then I would consider this a serious error in the language implementation, not to mention a pain in the backside :( Also, is it to much to ask that...
6
5887
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 classes that must implement an interface. All these classes must have a particular *static* property, and this in particular is handy not only to be...
17
2339
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 oriented design (owreilly - Juval Lowy), and it was actually very nice. The book goes on about how we should use Interfaces exposure instead of classes...
0
241
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 accessible'. In Delphi, I would use a class of static methods to enforce the singleton, and I added static fields to hold things like the database...
12
2561
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 advance, Joe
12
3420
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 static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to...
3
4350
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 globals are initialized and the second thread does not throw an exception. Thanks in advance. Adam Smith
0
7908
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...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
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...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
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...
0
6606
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...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.