如何将 Enum 与附加选项一起使用(全部、无)

发布于 2024-08-21 04:29:42 字数 274 浏览 2 评论 0原文

我有一个枚举,它:

  • 作为属性包含在我的类中
  • 它代表数据库表中的一些值(几种类型)
  • 它显示在 DropBox 中,以便它可以用作过滤器

现在我想添加此 DropBox 的“全部”(或“无”)值。

我应该如何执行此操作:

  • 将“全部”值添加到枚举?
  • 将“All”值添加到 DropBox,将我的属性类型从 Enum 更改为 String
  • 其他选项...

I have an enum, which:

  • is included in my class as a property
  • it represents some values from a database table (a couple of types)
  • it is displayed in DropBox, so that it can be used as a filter

Now I would like to add 'All' (or 'None' for example) value to this DropBox.

How should I do this:

  • add 'All' value to Enum?
  • add 'All' value to DropBox, change type of my property from Enum to String
  • some other option...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-08-28 04:29:42

Codesleuth 对另一个答案的评论让我再次阅读了这个问题,这是一个更新。

如果您要有多种组合。在您的情况下,这意味着选择任何类型组合都是有效的输入。

[Flags]
enum MyTypes
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    All = One | Two | Three | Four
}

如果用户只能选择一种类型或所有类型,则使用普通枚举:

enum MyType
{
    None,
    One,
    Two,
    Three,
    Four,
    All
}

Codesleuth comment on another answer made me read the question again and here is an update.

Consider the use of a flags enumeration if you are going to have multiple combination's. In your case it would mean that selecting any combination of types is a valid input.

[Flags]
enum MyTypes
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    All = One | Two | Three | Four
}

If the user can only select one type or all the types then use a normal enumeration:

enum MyType
{
    None,
    One,
    Two,
    Three,
    Four,
    All
}
御弟哥哥 2024-08-28 04:29:42

恕我直言,最好向您的枚举添加“全部”值,如下所示:

enum SampleEnum 
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    All = Value1 | Value2 | Value3 
}

这样,您就不必关心组合框中显示的项目,并且您可以对代码中该值的选择做出反应,如果这应该是必要的...

IMHO, it's best to add an 'All' value to your enum like so:

enum SampleEnum 
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    All = Value1 | Value2 | Value3 
}

This way, you won't have to care about the displayed items in your combobox, and you can react to the selection of that value in your code, if that should be necessary...

忆梦 2024-08-28 04:29:42

我还有另一个技巧,你可以在我的博客上查看: Enum Trick

最佳实践是将 None 或 Unknown 包含为 Zero(0)。

“全部”是计算得出的,即所有值的总和。

[Flags]
public enum MyTypes
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    Last,
    All = (Last << 1) - 3,
}

现在,当您添加值时,“全部”也会更新(无需更改)。

I have another trick, you can check it out at my blog: Enum Trick

Best practice is to include None or Unknown as Zero(0).

'All' is a calculated, as a sum of all values.

[Flags]
public enum MyTypes
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4,
    Four = 8,
    Last,
    All = (Last << 1) - 3,
}

Now, When you add values, 'All' is updated as well (No change is needed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文