简体   繁体   中英

Set flags enum conditionally

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM