Home > Software engineering >  how do i compare a string to an enum
how do i compare a string to an enum

Time:03-14

I have a problem where i get some status codes in string format, and after som datahandling, i have to persist those status codes also as strings.

Example of statuscodes is "0", "3"

But in between retrieving and saving data i have to handle the data in code. I also want my code to make sence to other members of my team, and future programmers. For that ive created an Enum with 4 statuscodes written in words.

Example of statuscodes enum. Resigned, Active.

Now enums are integers, so i cannot switch on the string statuscode and compare with enumvalues like ex.

// object.statuscode is a string.

switch(object.statuscode){
  case Enums.Statuscodes.Resigned:
    .
    .
    .
  case Enums.Statuscodes.Active:
    .
    .
    .
}

I am not in control of dataformat, but just been given the task of making the code more readable with enums.

Is there a way around this.

Ive tried something like [EnumMember(Value = "0")] Resigned . . .

and then case Enums.Statuscodes.Resigned: But that does not work.

Does anybody have an idea if this is possible or do i have to suggest that data should be retrieved and stored differently for this to work.

CodePudding user response:

An option would be to layout your enums similar to the receiving status codes and cast them appropriately.

enum StatusCode
{
    Resigned = 0,
    Active = 3,
    ...
    Undefined = 99999
}

Then, when retrieving the data as string, you have multiple options. One would be to double-cast it into the enum, like phuzi mentioned:

string statusCodeString;
StatusCode result = StatusCode.Undefined;

if (int.TryParse(statusCodeString, out int statusCodeInt))
{
    result = (StatusCode)statusCodeInt;
}

An alternative would be to buildup a dictionary beforehand, based on the enum values. This allows for a more direct "cast" and catches issues that are not being caught by the code above:

// This should be a static member somewhere
Dictionary<string, StatusCode> stringToStatusCode = new();

StatusCode[] allStatusCodes = (StatusCode[])Enum.GetValues(typeof(StatusCode));
foreach (StatusCode statusCode in allStatusCodes)
{
    stringToStatusCode.Add(((int)statusCode).ToString(), statusCode);
}

Now, you should be able to check against the dictionary. If the key is not in the dictionary, the code is undefined:

string statusCodeString;

if (!stringToStatusCode.TryGetValue(statusCodeString, out result))
    result = StatusCode.Undefined;

CodePudding user response:

As Max Play suggested, you may convert string input to enum. The other way round would be to convert enum to string as suggested here (enumStatusCode.ToString("D")).

CodePudding user response:

I think the solution where you parse the string statuscode to int in the switch, and then build cases on the enum values, is the most accesable. Thankyou for your time and help.

CodePudding user response:

I would do simple method if you have small count of enums, otherwise approache from Max Play with Dictionary is great option too

enum StatusCode
{
    Resigned = 0,
    Active = 3,
    Undefined = 999
}

public static StatusCode GetStatusCode(string code)
{
    switch (code)
    {
         case "0": return StatusCode.Resigned;
         case "3": return StatusCode.Active;
         default:
             throw new Exception($"Not valid status code: {code}");
           //return StatusCode.UNDEFINED;
     }
 }

//Example
if (GetStatusCode(object.statuscode) == StatusCode.Resigned)
{
    //Do what you want
}
  • Related