Set flags enum conditionally

680 Views Asked by At

I have an enum defined as follows

 [Flags]
public enum Roles
{
    [Descriptor("owner", "TheProficientLab representative")]
    OWNER = 0,

    [Descriptor("administrator", "Sets up users, questions, modules and reviews training and competency")]
    ADMIN = 1,

    [Descriptor("trainer", "Completes and submits training session with trainee")]
    TRAINER = 2,

    [Descriptor("trainee", "Completes training session with trainer")]
    TRAINEE = 3,

    [Descriptor("assessor", "Evaluates and submits competency assessments")]
    ASSESSOR = 4,

    [Descriptor("director or designee", "Approves training and competency")]
    DIRECTOR = 5
}

It is then a property in a class

public Common.Constants.Role.Roles Roles { get; set; }

How would I set that enum to multiple flags based on condition

Essentially I am looking for something like this

if (something)
   Item.Roles += Roles.ADMIN
if (something else)
   Item.Roles += Roles.ASSESSOR

Is there a += and -= equivalents in bitwise world?

1

There are 1 best solutions below

0
On BEST ANSWER

Per Idle_Mind's answer in the comments

Item.Roles |= Role.ADMIN to add

ItemRoles &= ~Role.ADMIN to remove

and the values need to be powers of 2 per docs