1

I have an enum:

[Flags]
public enum WeekDays
{

    Monday = 1,
    Tuesday = 2,
    Wednesday = 4,
    Thursday = 8,
    Friday = 16,
    Saturday = 32,
    Sunday = 64
}

And if I do:

var weekDays = WeekDays.Monday | WeekDays.Tuesday | WeekDays.Friday;

How can I check if a string for example Monday is set in the weekDays?

2
  • What are you trying to achieve? Commented Aug 27, 2015 at 20:48
  • Which bit's not working for you? Parsing a string, or checking if the relevant bit is set in a variable? Commented Aug 27, 2015 at 20:48

2 Answers 2

3

Check out the following code. You can use the HasFlag attribute:

class Program
    {
        [Flags]
        public enum WeekDays
        {

            Monday = 1,
            Tuesday = 2,
            Wednesday = 4,
            Thursday = 8,
            Friday = 16,
            Saturday = 32,
            Sunday = 64
        }

        private static string result;

        static void Main()
        {
            var wd = new WeekDays();
            Console.WriteLine(wd.HasFlag(WeekDays.Monday));

            wd = WeekDays.Monday;

            Console.WriteLine(wd.HasFlag(WeekDays.Monday));

            bool is_defined = Enum.IsDefined(typeof(WeekDays), "Monday");

            Console.WriteLine(is_defined);

            bool is_not_defined = Enum.IsDefined(typeof(WeekDays), "Mondays");

            Console.WriteLine(is_not_defined);

            bool has_flag_by_string = Enum.TryParse<WeekDays>("Monday", out wd);

            Console.WriteLine(has_flag_by_string);

            Console.ReadLine();
        }
    }

EDIT

Added the ability to check if attribute exists by string.

EDIT 2

Added the Enum.TryParse<> to parse the string value.

Reference:

https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

4 Comments

I think the OP is saying how do they know that given the string "Monday" that the corresponding enum value is set in a give WeekDays value.
@juharr added the TryParse to get what he want based on your comment.
FYI you don't have to include the generic type in the TryParse call as it will be inferred from the out parameter.
Very true, I do it out of habit and readability so I know what Type I'm working with.
2

I think you want this

WeekDays days = WeekDays.Monday | WeekDays.Tuesday;

string monday = "Monday";

WeekDays day;
if (Enum.TryParse(monday, true, out day))
{
    if (days.HasFlag(day))
    {
        Console.WriteLine("Has {0}", monday);
    }
    else
    {
        Console.WriteLine("Does not have {0}", monday);
    }
}
else
{
    Console.WriteLine("invalid string");
}

First you can use Enum.TryParse to parse the string to an enum value or determine if the string is invalid. Then use HasFlag to see if it is included.

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.