3

I have the following enum:

public enum DocumentState
{
    Default = 0,
    Draft = 1,
    Archived = 2,
    Deleted = 3
}

Most places in my solution I use it as a normal enum. Some places I use at an int casting it like this:

(int)DocumentState.Default)

But then, some places, as for example when I work with Examine (who only accepts strings, and not ints as inputs), I need to pass on the enumns int value as if it was a string. This can be done in the following way:

((int)DocumentState.Default).ToString()

My question is now; can it really be true that there is no other way to retrieve the enums value as a string?

I know that I might be abusing the Enum, but sometimes this is the best approach in the given case.

Squazz
  • 3,912
  • 7
  • 38
  • 62
  • Does `DocumentState.Default.ToString()` not work? – markpsmith Apr 08 '16 at 11:43
  • 1
    DocumentState.Default.ToString() returns "Default" – Squazz Apr 08 '16 at 11:44
  • 1
    Not really. what is so bad with this/your approach. You could drop enums and use a Dictionary instead – Jehof Apr 08 '16 at 11:44
  • 2
    I tend to agree that you're abusing Enums here. – Drew Kennedy Apr 08 '16 at 11:46
  • @Jehof it just seemed kinda wrong when I wrote it. As others have said, it felt like I was abusing the Enum – Squazz Apr 08 '16 at 11:48
  • 1
    Why do you need to use the int value of the enum at all? Maybe you should consider to use a real class which has a `DocumentState`- and an `int` property, for example `ID`. If you need that as string a `document.Id.ToString()` is very readable and reliable – Tim Schmelter Apr 08 '16 at 11:49
  • There is an Enum.GetValues that return an array of integers, but difficult to see any usefulness here – Steve Apr 08 '16 at 11:55
  • Are you open to options other than Enums? Or are you so entrenched in your code that a move to something else would be far too time consuming? – Drew Kennedy Apr 08 '16 at 11:58
  • @DrewKennedy: yes, but it depends on what purpose the int value should have – Tim Schmelter Apr 08 '16 at 11:59
  • @DrewKennedy Other options them Enums might also be viable yes. I just don't have the complete overview over de implications of a shift from Enums as I'm not the original developer of that part of the code – Squazz Apr 08 '16 at 12:00
  • @DrewKennedy: added [an example](http://stackoverflow.com/a/36499206/284240) of a class. Much more code but very easy to maintain and to extend if you need it. So more robust and resuable than a simple enum. You could also simplify it by storing the `StateId` directly in the document class. – Tim Schmelter Apr 08 '16 at 12:13

5 Answers5

9

Use DocumentState.Default.ToString("d"). See https://msdn.microsoft.com/en-us/library/a0h36syw(v=vs.110).aspx

0

It seems as if you abuse enums. If you need to store additional informations use real classes. In this case your Document-class could have a State-property that returns an instance of a DocumentState class which has a property for the DocStateType-enum. Then you could add additional informations like a TypeId if necessary and the code to get a string is very simple and readable:

public class Document
{
    public int DocumentId { get; set; }
    public DocumentState State { get; set; }
    // other properties ...
}


public enum DocStateType
{
    Default = 0,
    Draft = 1,
    Archived = 2,
    Deleted = 3
}

public class DocumentState
{
    public DocumentState(DocStateType type)
    {
        this.Type = type;
        this.TypeId = (int) type;
    }
    public DocumentState(int typeId)
    {
        if (Enum.IsDefined(typeof (DocStateType), typeId))
            this.Type = (DocStateType) typeId;
        else
            throw new ArgumentException("Illegal DocStateType-ID: " + typeId, "typeId");
        this.TypeId = typeId;
    }

    public int TypeId { get; set; }
    public DocStateType Type { get; set; }
    // other properties ...
}

If you want the TypeId as string you just need doc.State.TypeId.ToString(), f.e.:

Document doc = new Document();
doc.State = new DocumentState(DocStateType.Default);
string docTypeId = doc.State.TypeId.ToString();

This approach uses the enum value as TypeId, normally you wouldn't use the enum value for your business logic. So they should be independent.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

Personally, I think what you're trying to do with your Enum isn't the greatest approach. What you could do is create a class that has a Dictionary<TKey, TValue> to hold your keys and values.

In C# 6 or later:

static class DocumentState {
    public static Dictionary<string, int> States { get; } = new Dictionary<string, int>() { { "Default", 0 }, { "Draft", 1 }, { "Archived", 2 }, { "Deleted", 3 } };
}

C# 5 or lower:

class DocumentState {
    public Dictionary<string, int> State { get; }

    public DocumentState() {
        State = new Dictionary<string, int>() { { "Default", 0 }, { "Draft", 1 }, { "Archived", 2 }, { "Deleted", 3 } };
    }
}

This way you can call your dictionary key at any time to retrieve the desired value, and not mistakenly override the default value of the Dictionary.

Shiva
  • 20,575
  • 14
  • 82
  • 112
Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34
-2

You can use Enum.GetName method

Doliveras
  • 1,794
  • 2
  • 14
  • 30
-3

You can reference System.Runtime.Serialization and use the EnumMember attribute.

public enum foonum
{
    [EnumMember(Value="success")]
    success,
    [EnumMember(Value="fail")]
    fail
}

Console.WriteLine (foonum.success);

yields: "success"

Crowcoder
  • 11,250
  • 3
  • 36
  • 45