473,396 Members | 1,785 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,396 software developers and data experts.

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(string 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(string msg)
{
....
}

LogWarning(ErrorStrings.ThisIsMyError);

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 1675
* 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(string 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(string msg)
{
...
}

LogWarning(ErrorStrings.ThisIsMyError);

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
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),...
1
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 = ...
0
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...
5
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...
1
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: ...
2
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...
4
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...
11
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....
1
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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,...
0
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...
0
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,...

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.