0

I have an enum class as follows:

namespace Import.DataObject
{
    internal enum Status
    {
        FullTime,
        Terminated,
    }
}

In my main program I have a DB call then a

  foreach (ClientStatus status in databaseStatus)
            {
                if (status.Name == "FullTime")
                {
                }
                if (status.Name == "Terminated")
                {
                }

            }

How would I set the enum based on the if statements? The Name has an ID associated with it, and I want that ID to = the respecitve name in the enum, I have both the name and id available within the if statement.

1 Answer 1

2

Use Enum.Parse:

Status val = (Status)Enum.Parse(typeof(Status), status.Name);

If you're not sure the string is a valid enum, use Enum.TryParse:

Status? val;
if (!Enum.TryParse<Status>(status.Name, out val))
    val = null;

Then just cast to (int) to get the number value:

status.ID = (int)val;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.