简体   繁体   中英

How to use an Enum[]?

In my project i am using an Array of bool which defines the user's access rights. For example

public bool[] Security {get; set;}

where

[0] = Admin
[1] = GrantWrites
[2] = GrantDeletes
[3] = User

It is working quite well. I would set it to {F,T,F,T} or {0,1,0,1} and that particular user gets access as a User and it allows him to write.

I am trying to convert it to an enum but apparently i would need an array of it.

currently i have the following (not working)

public class UserCrops
{
    public UserCrops(etc.., Enum[] _Security)
    {
    .
    .
    .
        Security = _Security;
    }
    .
    .
    .
    public Enum[] Security
    {
        Admin,
        GrantWrites,
        GrantDeletes,
        User
    }

}

I found some links like this but no help.

Thanks in advance

Edit: Both answers are very well explained but I am going with the non-Flag one just because it seems easier for me :)

Edit2: How can i create a new object (outside of class?) I used to do

bool[] security = new bool[9];
for (int i = 0; i < 9; i++)
    {
    security[i] = chklstSecurity.Items[i].Selected;
}
userCropList.Add(new UserCrops(.., txtBiologicalAssessmentApprovalDate.Text, security));

But now?

Try with:

[Flags]
public enum Security
{
    Admin = 1,
    GrantWrites = 2,
    GrantDeletes = 4,
    User = 8
}

And you'll use it like this:

Security security = Security.GrantWrites | Security.GrantDeletes;

if ((security & Security.GrantWrites) == Security.GrantWrites)
{
}

Comparison can be simplified as pointed out by pswg to increase its readability. Moreover I suggest to include a default value in the enum (for when variable is not initialized):

[Flags]
public enum Security
{
    None = 0,
    Admin = 1,
    GrantWrites = 2,
    GrantDeletes = 4,
    User = 8
}

Finally note that you can provider shortcut for common combinations of flags:

[Flags]
public enum Security
{
    // Other values
    FullAccess = Admin | GrantWrites | GrantDeletes
}

More of that on MSDN . Please note this approach mimics attributes for file/directories in file system (and many other). IMO is much simpler to use than keep an array of enums as suggested in the other answer :

  • You do not have to search entire array to check if a permission is granted or not.
  • You do not have to check for a null value ( enum can't be null , an array can be).
  • It uses less space (even if nowadays this is not so important).
  • It's naturally (more) safe so less checks are needed (for example to avoid duplicates inside array).
  • It can be easy stored (as text or integer without additional code).

But it has, compared to that, two main drawbacks:

  • Flags are finite (32 if you're using an Int32 for your enum or 64 for an Int64 ).
  • You can't easily switch to something else (if, for example, Security has to become a class you'll need to write much more code to mimic enums syntax and some assumption made by code when working with enums will be broken).

Remove the [] and use enum instead of Enum :

public enum Security
{
    Admin,
    GrantWrites,
    GrantDeletes,
    User
}

And you probably want to use Security[] as a method parameter:

public UserCrops(etc.., Security[] _Security) 

Using flags (as Adriano suggests ) is an excellent suggestion too, but it will require you to rethink how you're storing your permissions. Instead of storing an array of bool 's, you'll represent the entire security set as a single value, with different bits representing each permission.

Read Enumeration Types (C# Programming Guide) under the section Enumeration Types as Bit Flags for more information.

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