简体   繁体   English

如何使用枚举[]?

[英]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. 我将其设置为{F,T,F,T}或{0,1,0,1},并且该特定用户可以作为User获得访问权限,并允许他进行写操作。

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 Edit2:我如何创建一个新的对象(类之外)?

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. pswg指出可以简化比较以提高其可读性。 Moreover I suggest to include a default value in the enum (for when variable is not initialized): 此外,我建议在enum包含一个默认值(用于未初始化变量的情况):

[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 . 有关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 : IMO比保留另一个答案中建议的枚举数组简单得多:

  • 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). 您不必检查是否为null值( enum不能为null ,数组可以是)。
  • 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 ). 标志是有限的(如果您将Int32用于enum ,则为32;对于Int64使用64)。
  • 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). 您不能轻易切换到其他内容(例如,如果必须将Security变成一个类,则您需要编写更多代码来模仿枚举语法,并且使用枚举时由代码做出的某些假设将被破坏)。

Remove the [] and use enum instead of Enum : 删除[]并使用enum代替Enum

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

And you probably want to use Security[] as a method parameter: 您可能希望将Security[]用作方法参数:

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. 使用标志(如Adriano所建议的 )也是一个很好的建议,但它需要您重新考虑如何存储权限。 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. 不用存储bool数组,而是将整个安全性集表示为单个值,用不同的位表示每个权限。

Read Enumeration Types (C# Programming Guide) under the section Enumeration Types as Bit Flags for more information. 有关更多信息,请阅读 枚举类型作为位标志 ”部分下的枚举类型(C#编程指南)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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