473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enforcing (forcing) Resource Strings

Is there a way in C# to have the compiler enforce that users use
Resource Strings?

Say I have a logging subsystem, and I want make sure that the only
string values that get logged come from a resource file, can I do
that?

I know I can use:

static void LogWarning(stri ng val)
{
...
string toLog = ResourceManager .GetString(val) ;
}

But that is problematic: At compile time, I have no idea if val
exists.

I can also use:

static void LogWarning(stri ng msg)
{
....
}

LogWarning(Erro rStrings.ThisIs MyError);

But nothing stops users of this log system from passing in a hardcoded
string into my LogWarning method.

Ideally I want something that is strongly typed, but can't be
overloaded with a string.

Any ideas?

Jun 28 '07 #1
1 1684
* Alan wrote, On 28-6-2007 21:56:
Is there a way in C# to have the compiler enforce that users use
Resource Strings?

Say I have a logging subsystem, and I want make sure that the only
string values that get logged come from a resource file, can I do
that?

I know I can use:

static void LogWarning(stri ng val)
{
...
string toLog = ResourceManager .GetString(val) ;
}

But that is problematic: At compile time, I have no idea if val
exists.

I can also use:

static void LogWarning(stri ng msg)
{
...
}

LogWarning(Erro rStrings.ThisIs MyError);

But nothing stops users of this log system from passing in a hardcoded
string into my LogWarning method.

Ideally I want something that is strongly typed, but can't be
overloaded with a string.

Any ideas?
There is an FxCop rule to enforce this, if you're using Visual Studio
Team System Developer or Suite you can enforce these rules before check-ins.

There is no way however to make every string parameter unsuitable for a
strings. You can make a resource file type safe in .NET 2.0. If you open
a resource file and look in the information/message tab there should be
a link there to generate a typed class for that specific resource file.

If you're still using .NET 1.1 you can use the String Resource Tool. But
I don't know where it moved to with the close down of Got Dot Net.
Here's a post about it:

http://blogs.msdn.com/scottdensmore/...08/436867.aspx

I hope this helps,

Jesse
Jun 28 '07 #2

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

Similar topics

1
4139
by: Scott Smith | last post by:
Several attributes and interface methods I'm using refer to resources (such as strings or icons) by a numeric ID. This was the only way to refer to resources in unmanaged code (C++, for example), but I'm writing in C# now, and it seems that resources are named with string identifiers. Visual Studio doesn't seem to like resource names such as "100", so I'm wondering how this could work. Is it absolutely necessary for me to have a...
1
1587
by: John Stemper | last post by:
Getting resource files to work with VS2002 and ASP.Net seems to be a little flaky. I've added a resource file "strings.resx" to the project. The properties of the files say Build action = "embedded resource". I've added the appropriate code to the Global.asax: protected void Application_Start(Object sender, EventArgs e) { Application = new ResourceManager("strings",Assembly.GetExecutingAssembly()); }
0
1075
by: PontiMax | last post by:
Hi, when we used VB 6 to build all kinds of applications we applied the following technique to automatically assign resource strings to ui controls: Each control has a unique resource id (which we store in the <Tag> property). When a form is loaded/displayed, a function is called which cycles through all form controls, assesses their resource ids (by means of the <Tag> property), fetches the corresponding resource strings, and finally...
5
2607
by: Martin Bischoff | last post by:
Hi, is it possible to implement custom resource managers for ASP.NET 2.0 so that strings can be read from a database (for example)? Ideally, it should be possible to configure the custom resource manager in web.config, so that it replaces the default resource manager. Using the <%$ ... %> syntax should read the strings via the custom resource manager, e.g. when used like this:
1
1838
by: Dirc Khan-Evans | last post by:
I am trying to localise my app using embedded resource and sattellite assemblies. I can get it working using the following if I have a resource file with the same name as the class: ResourceManager resourceManager = new ResourceManager(this.GetType()); However, I want to use a global resource for my assembly.
2
5519
by: Fredrik Rodin | last post by:
All, I'm having problems with my resource manager in ASP.NET 2.0 after conversion from ASP.NET 1.1. Here is a background: In ASP.NET 1.1 All my user controls and aspx pages inherit from base classes. A base class includes this property (among others...;-)):
4
1748
by: dwyer.bill | last post by:
My boss insists that whenever we use a string we place it in a resource file every single time. This goes for error string that are displayed through a message box, constants, ect. He claims that this adds a huge performance boost in the application but I don't understand why. Is there something to this? Bill
11
1733
by: Jon Mcleod | last post by:
I'm trying to wrap my ming around C#/CLR garbage collection. In an ASP.NET project, I'm having a problem because an object destructor is being called by another thread, long after my code is done. I'm wanting to know if I can force it to happen immediately (when the object goes out of scope) without going back and adding all of the required Dispose() calls.. public Method() { CustomClass o = new CustomClass(..);
1
6991
by: TisMe | last post by:
Hi All, I am trying to use resource files to globalize my website application. I had this working fine, working through the API for setting page titles programatically, with the resource files in /resources I then started to add declarative code to the web forms to controls such as buttons and labels, as follows: <asp:Label ID="lblHowFindOut" runat="server" Text="<%$ Resources:how_did_you_find_out_about_this_site %>" />
0
7947
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7880
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8374
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...
0
8242
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5739
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
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
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
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
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.