design question about the use of constants and resource files

G

gabriel

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
 
J

Jon Skeet [C# MVP]

gabriel said:
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.
 
G

Guest

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
 
G

gabriel

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top