Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# String Resource Values as Enum String Part values?

Using VS2010 and .net V4.0 I would like to achieve the following:

I already have 2 resource files in my project for 2 languages - English and Czech.

I must say Resource Management in .net is excellent, I am suprised even to get code completion when implementing a String for example:

string desc = Strings.ResourceManagerDesc

This gets the string associated with the current culture of the thread.

Now I am trying to create an Enum that can have the String portion of the Enum interpreted from the Strings resources. In the following way (This code DOES NOT WORK):

public enum DownloadStatus
{
 1 = Strings.DownloadState_Complete,
 2 = Strings.DownloadState_Failed,
 3 = Strings.DownloadState_InProgress
}

This is a made up example, but you can see the point here. Since the above code won't work, is there a best practice way to achieve what I want?

like image 354
JL. Avatar asked Jun 04 '10 10:06

JL.


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

This question is old but does not have an accepted answer and I was looking for a good way to do this.

Here's what I did, I built an extension to my Enum so that it returns a value from the Resource Manager :

public enum EventType
    {
        NewVersion = 1,
        Accepted = 2,
        Rejected = 3,
        BruteForce = 4
    }

    public static class EventTypeExtension
    {
        public static string Display(this EventType type)
        {
            return Strings.ResourceManager.GetString("EventType_" + type);
        }
    }

I hope this can help someone!

like image 139
VinnyG Avatar answered Sep 21 '22 16:09

VinnyG


Enums cannot inherit from strings. In code you do not need to be concerned with the language of the code, so your enum can simply contain the relevant states.

Looks like what you need is a utility method to convert the enum value to the relevant string representation - simply make a method for this.

EDIT: when you use an enum to switch on cases, but need further information per enumerated value, I tend to drop the enum and create a host of static references and use those in the check instead. This reference can be a class wrapping an enum value, which could then expose helpful titles or descriptions.

like image 37
Adam Houldsworth Avatar answered Sep 22 '22 16:09

Adam Houldsworth