473,856 Members | 1,773 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 2233
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
17198
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
6606
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
2435
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 the method Type.GetTypeFromCLSID be documented
6
5898
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 consequent with the fact that you can do it with classes but also with the fact that you cannot...
17
2375
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 (this is my terminology and english is not my language so I hope you understand what I'm on about...).
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 connections, etc. This worked fine in Delphi because we have unit initialisation/finalisation sections...
12
2593
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
3446
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 ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
3
4363
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
11056
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
10696
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...
1
10782
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9531
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
7932
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
5761
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4575
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
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3201
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.