[英]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比保留另一个答案中建议的枚举数组简单得多:
null
value ( enum
can't be null
, an array can be). 您不必检查是否为null
值( enum
不能为null
,数组可以是)。 But it has, compared to that, two main drawbacks: 相比之下,它有两个主要缺点:
Int32
for your enum
or 64 for an Int64
). 标志是有限的(如果您将Int32
用于enum
,则为32;对于Int64
使用64)。 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.