473,407 Members | 2,315 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,407 software developers and data experts.

design question about the use of constants and resource files

Greetings,

I am working on a project and cannot choose the best way to achieve this :

I got a method which returns an error code like this : DISK_FULL or
PERMISSION_DENIED.

Those are constants defined in a class named "Constants" :

public static byte DISK_FULL = 1;
public static byte PERMISSION_DENIED = 2;

Now I have a resource file with keys like this :

DISK_FULL Sorry, your disk is full, should make some room.
PERMISSION_DENIED Sorry, bla bla.
But since they are constants, the keys are not retrieved (I try to
retrieve "1" for instance), of course.

I thought to design my resource file like this :

1 Sorry, your disk is full, should make some room.
2 bla bla

My dilema is that my file is not very human-readable that way :(
I then thought but I could make things like this :
public static string DISK_FULL = "DISK_FULL";
public static string PERMISSION_DENIED = "PERMISSION_DENIED";

And a resource file with keys like this :

DISK_FULL Sorry, your disk is full, should make some room.
PERMISSION_DENIED Sorry, bla bla.
But I find it useless to use strings here : bytes are shorter so faster
managed (yes this does not change everything, I know ;) )

My question : what do you do ??

phew, he finally asked the question ;)
I come from the java world so maybe I am missing here :)

Thank you for your patience

Nov 17 '05 #1
4 2546
gabriel <sp**@yahoo.fr> wrote:
I am working on a project and cannot choose the best way to achieve this :

I got a method which returns an error code like this : DISK_FULL or
PERMISSION_DENIED.

Those are constants defined in a class named "Constants" :

public static byte DISK_FULL = 1;
public static byte PERMISSION_DENIED = 2;
Firstly, the .NET naming conventions would suggest DiskFull and
PermissionDenied. I've converted to using these conventions in my Java
code too, and they do make things much more readable.
Now I have a resource file with keys like this :

DISK_FULL Sorry, your disk is full, should make some room.
PERMISSION_DENIED Sorry, bla bla.
But since they are constants, the keys are not retrieved (I try to
retrieve "1" for instance), of course.

I thought to design my resource file like this :

1 Sorry, your disk is full, should make some room.
2 bla bla

My dilema is that my file is not very human-readable that way :(


The easy way of dealing with this is to use an enum. You can easily get
the name of an enum element, you get the speed of ints (or whatever -
although frankly as you'd only be passing a reference round, I think
your performance concern is misplaced), and you keep the human
readability of the file.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
If you can move to Visual Studio 2005, then you will find that the IDE
provides strong support for resources such as these.

It provides a graphical table where you enter your constant/string pairs...

DiskFull Your disk is full
PermissionsError There was a permissions error

It generates a class from this which provides a static "get" property for
each resource. The class is in the Properties namespace.

An example of using it...

MessageBox.Show(Properties.Resources.DiskFull);

.....displays the string "Your disk is full"

VS 2003 provides similar features, but they are somewhat hidden behind
localization. I haven't used them myself, but I think that with a little bit
of investigation will find what you need. Start in the Solution Explorer,
right-click the project, and select "Add Component" then "Add Assembly
Resource File", and take it from there.

You can also use a HashTable

enum ErrorCodes
{
DiskFull,
PermissionsError
}

Hashtable errorTable = new Hashtable();

// Initialize the hash table
errorTable.Add(ErrorCodes.DiskFull, "Your disk is full");
errorTable.Add(ErrorCodes.PermissionsError, "Permissions error");

// Use an item from the hash table
MessageBox.Show(errorTable[DiskFull].ToString());

Regards,

Javaman


Nov 17 '05 #3
Hi,

As Jon mentioned, I use enums.

You might want to check out a simple method I use for this purpose:
http://rakeshrajan.com/blog/archive/2005/10/11/43.aspx

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.rakeshrajan.com/
"gabriel" wrote:
Greetings,

I am working on a project and cannot choose the best way to achieve this :

I got a method which returns an error code like this : DISK_FULL or
PERMISSION_DENIED.

Those are constants defined in a class named "Constants" :

public static byte DISK_FULL = 1;
public static byte PERMISSION_DENIED = 2;

Now I have a resource file with keys like this :

DISK_FULL Sorry, your disk is full, should make some room.
PERMISSION_DENIED Sorry, bla bla.
But since they are constants, the keys are not retrieved (I try to
retrieve "1" for instance), of course.

I thought to design my resource file like this :

1 Sorry, your disk is full, should make some room.
2 bla bla

My dilema is that my file is not very human-readable that way :(
I then thought but I could make things like this :
public static string DISK_FULL = "DISK_FULL";
public static string PERMISSION_DENIED = "PERMISSION_DENIED";

And a resource file with keys like this :

DISK_FULL Sorry, your disk is full, should make some room.
PERMISSION_DENIED Sorry, bla bla.
But I find it useless to use strings here : bytes are shorter so faster
managed (yes this does not change everything, I know ;) )

My question : what do you do ??

phew, he finally asked the question ;)
I come from the java world so maybe I am missing here :)

Thank you for your patience

Nov 17 '05 #4
> The easy way of dealing with this is to use an enum. You can easily get
the name of an enum element, you get the speed of ints (or whatever -
although frankly as you'd only be passing a reference round, I think
your performance concern is misplaced), and you keep the human
readability of the file.

Thank you, I'll dig that !

humbly yours :)

Nov 17 '05 #5

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

Similar topics

9
by: אלחנן | last post by:
hi.. i don't know if this is the right group for this.. i have a small application which involves a windows service, and web services which bascally retrieves files, process them and zips them...
3
by: MLH | last post by:
I make API calls to the sndPlaySound function, something like this... XX% = sndPlaySound(msound, MyParm) Several predefined system constants (or API intrinsic constants) have been recommended...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
6
by: Kevin | last post by:
Has anyone ever encountered this problem? (I searched on google for past posts but didn't find anything) I have a class implementing the IHttpModule Interface. It's mainly used for seting up...
9
by: Alf P. Steinbach | last post by:
<what to design a C++ solution for> A Windows API /resource/ is data embedded in the executable, accessed via API functions. A resource is completely identified by the quadruple (id, type,...
1
by: Jason | last post by:
Greetings , I'm studying for a MS exam and I'm a little confused about assemblies. Not what they are but just a specific comment that I'm reading and it doesn't make sense to me. It reads ...
3
by: www.gerardvignes.com | last post by:
I do most JavaScript programming with objects and methods. I use tiny boot and shutdown scripts as hooks for the browser load and unload events. My JavaScripts interact with scripts running on...
6
by: Stephen Torri | last post by:
I am trying to produce a singleton class that I can use throughout my library to write tracing information to a file. My intent was to design such that someone using the library in its debug mode...
27
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 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
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
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,...
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 projectplanning, 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.