How can I return a bitmask value from an enumeration with a flag attribute in C #?

Our database has a bitmask that represents what types of actions a user can do.

In our C # client, when we get this integer value from the database, we create enum / flag. It looks something like this:

[Flags]
public enum SellPermissions
{
    Undefined = 0,
    Buy = 1,
    Sell = 2,
    SellOpen = 4,
    SellClose = 8
    // ...
}

In our application, I have a permission rights page, which I then use to change the value of this enum using bitwise OR for different enumeration values.

permissions = SellPermisions.Buy | SellPermissions.Sell;

Now, after making these changes, in my database call, I need to call update / insert sproc, which expects an integer value.

How to get the binary value of an integer from my enum / flag so that I can set the changed permissions in the database?

+3
2

, int.

int newPermissions = (int)permissions.
+7
int permissionsValue = (int) permissions;
+2

Source: https://habr.com/ru/post/1748362/


All Articles