如何将 Enum 与附加选项一起使用(全部、无)
我有一个枚举,它:
- 作为属性包含在我的类中
- 它代表数据库表中的一些值(几种类型)
- 它显示在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Codesleuth 对另一个答案的评论让我再次阅读了这个问题,这是一个更新。
如果您要有多种组合。在您的情况下,这意味着选择任何类型组合都是有效的输入。
如果用户只能选择一种类型或所有类型,则使用普通枚举:
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.
If the user can only select one type or all the types then use a normal enumeration:
恕我直言,最好向您的枚举添加“全部”值,如下所示:
这样,您就不必关心组合框中显示的项目,并且您可以对代码中该值的选择做出反应,如果这应该是必要的...
IMHO, it's best to add an 'All' value to your enum like so:
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...
我还有另一个技巧,你可以在我的博客上查看: Enum Trick
最佳实践是将 None 或 Unknown 包含为 Zero(0)。
“全部”是计算得出的,即所有值的总和。
现在,当您添加值时,“全部”也会更新(无需更改)。
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.
Now, When you add values, 'All' is updated as well (No change is needed).