根据上下文标记具有共享值的枚举?
我想设计一个标志枚举,但由两个用例共享:
- 用例 1:存储是/否答案。
- 用户案例2:存储多项选择答案。
例如: 我定义了以下内容
[Flags]
public enum Answer
{
Yes = 1,
No = 2,
Choice1 = 1,
Choice2 = 2,
Choice3 = 4,
Choice4 = 8,
}
组合它的原因是共享一个一致的接口(例如过程答案)。对于这个设计有什么异议吗? 谢谢!
I want design a flag enum but shared by two use cases:
- Use case 1: to store yes/no answer.
- User case 2: to store multiple choice answer.
For example: I have defined the following
[Flags]
public enum Answer
{
Yes = 1,
No = 2,
Choice1 = 1,
Choice2 = 2,
Choice3 = 4,
Choice4 = 8,
}
The reason to combine this is to share a consistent interface(e.g. process answer). Are there any arguments against this design?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会这样做。原因如下:
结果是
“是”
这是有道理的,而且显然它在语法上是正确的,但是你让维护这样的枚举变得非常困难。它将匹配枚举的第一个正确值。我相信这是可以改进的逻辑,从而限制/消除混乱。
我会这样做吗? 不。
I wouldn't do this. Here's why:
The result of that is
"Yes"
It makes sense, and obviously it is syntactically correct, but you are making it very hard to maintain an enumeration like that. It is going to match on the first correct value of the enumeration. That is logic that I believe can be improved upon so that confusion is limited/eliminated.
Would I do this? No.
1)如果你想检查问题是否被回答,那么你可以检查值1(如果值1表示没有回答)。
2) 如果设置了任何选项,那么您可以假设您的答案正在被回答。
希望这有帮助!
1) If you want to check whether the question is being answered or not then you can check for value 1 (if the value 1 Means it is not answered).
2) If any of the choices is set then you can assume that your answered is being answered.
Hope this helps!