[英]enum combination - vb.net
I have a project in Vb.net and windows form application with dotnet framework 3.5. 我在Vb.net中有一个项目,并在Windows窗体应用程序中使用了dotnet framework 3.5。 I am using Enum for storing and processing tasks As :
我正在使用枚举存储和处理任务,如:
Public Enum TaskStatus
none = 0
completed = 2
executing = 4
executed = 8
errors = 16
uploaded = 32
incomplete = 64
all =-1
End Enum
Private Function Manage()
Dim ts As TaskStatus = TaskStatus.none
'' Task Performed with Error
ts = TaskStatus.errors Or TaskStatus.executed
'' Task Uploading
ts = ts Or TaskStatus.uploaded
ts = ts Or TaskStatus.completed
CheckStatus(ts)
End Function
Private Function CheckStatus(ByVal ts As TaskStatus)
'' Now i have to check for two or More Enums
'' ts have current values := Error , Executed, Uploaded , Completed
If ts = (TaskStatus.uploaded Or TaskStatus.errors) Then
'' It is not working
End If
If ts = (TaskStatus.executed Or TaskStatus.uploaded Or TaskStatus.completed) Then
'' It is not working
End If
If ts And (TaskStatus.uploaded Or TaskStatus.executed) Then
'' Working, But Not Sure is it correct way ?
End If
If (ts And TaskStatus.completed) = TaskStatus.completed Then
'' Completed
End If
End Function
Since ts have current values := Error Or Executed Or Uploaded Or Completed. 由于ts具有当前值:=错误或已执行或已上载或已完成。
I can not use HasFlag
due to framework 3.5. 由于框架3.5,我无法使用
HasFlag
。
How to compare If enum contains two or more specific values in it, as described in code ? 如何比较如代码中所述, 枚举中是否包含两个或多个特定值?
To check for two flags while ignoring any other flags, perhaps something like: 在忽略其他任何标志的情况下检查两个标志,可能类似于:
If (ts And (TaskStatus.uploaded Or TaskStatus.errors)) =
(TaskStatus.uploaded Or TaskStatus.errors) Then
' ...
End If
which is pretty ugly. 这很丑。 A helper method would make things a bit better:
辅助方法可以使事情变得更好:
Function HasFlags(value As TaskStatus, flags As TaskStatus) As Boolean
Return (value And flags) = flags
End Function
If HasFlags(ts, TaskStatus.uploaded Or TaskStatus.errors) Then
' ...
End If
You can pass in as many flags as you want to check for. 您可以传递要检查的任意多个标志。
If the completed
bit is high, then it is completed. 如果
completed
位为高,则完成。 It doesn't matter which other bits are high. 其他哪个位高都无关紧要。
You could complete with errors and have a value of 18 (completed + errors). 您可以完成但有错误,值为18(已完成+错误)。 It's still complete, and completed with errors.
它仍然是完整的,并且有错误。
If your program doesn't work with this logic, then you are using status bits incorrectly. 如果您的程序不适用于此逻辑,则说明您在错误使用状态位。
Also 也
all
is silly because all should be the sum of all your enum values all
都是愚蠢的,因为all应该是您所有枚举值的总和 If you had defined your enum like this, 如果您是这样定义枚举的,
Public Enum TaskStatus
completed = 1
executing = 2
executed = 4
errors = 8
uploaded = 16
End Enum
all
will now be 31 all
将是31 incomplete
is when TaskStatus.Complete
bit is 0 TaskStatus.Complete
位为0时incomplete
Here is how you could design it better and an effective usage example 这是如何更好地设计它以及有效的使用示例
Public Enum TaskStatus
completed = 1
executing = 2
executed = 4
errors = 8
uploaded = 16
End Enum
Sub Main()
CheckStatus(TaskStatus.executing + TaskStatus.uploaded)
CheckStatus(TaskStatus.completed + TaskStatus.errors)
CheckStatus(TaskStatus.completed)
CheckStatus(TaskStatus.completed + TaskStatus.executing + TaskStatus.executed + TaskStatus.errors + TaskStatus.uploaded)
Console.ReadLine()
End Sub
Private Sub CheckStatus(ByVal ts As TaskStatus)
Console.WriteLine("TaskStatus value: {0}", ts)
If ts And TaskStatus.completed Then
If ts And TaskStatus.errors Then
Console.WriteLine("completed with errors")
Else
Console.WriteLine("completed without errors")
End If
Else
Console.WriteLine("not completed")
If ts And TaskStatus.executing Then
Console.WriteLine("still executing")
If ts And TaskStatus.uploaded Then
Console.WriteLine("finished uploading")
Else
Console.WriteLine("still uploading")
End If
End If
End If
If ts = 31 Then
Console.WriteLine("you set all the status bits!")
End If
Console.WriteLine()
End Sub
Output with test program: 输出与测试程序:
TaskStatus value: 18
TaskStatus值:18
not completed没完成
still executing仍在执行
finished uploading上传完毕
TaskStatus value: 9TaskStatus值:9
completed with errors错误完成
TaskStatus value: completedTaskStatus值:已完成
completed without errors完成没有错误
TaskStatus value: 31TaskStatus值:31
completed with errors错误完成
you set all the status bits!您设置所有状态位!
Think about why I removed incomplete
. 想一想为什么我删除了
incomplete
。 Each bit represents a boolean logical state ie it can only be one way or another. 每一位代表一个布尔逻辑状态,即它只能是一种方式。 If you have a bit for completed and incomplete, then you could potentially have an illogical state where completed and incomplete are both high, a value of 66 + any or all other bits in your example (with the exception of
all
and none
- also why I removed those). 如果您有一点要完成和不完整,那么您可能会处于不合逻辑的状态,其中完成和不完整都很高,示例中值为66 +任何或所有其他位(除了
all
和none
也是为什么)我删除了那些)。 The state of complete and incomplete is undefined, so why waste the bit? 完整和不完整的状态是不确定的,那么为什么要浪费一点呢?
Executing and executed offer a similar conundrum, as not all combinations make sense, such as executing + executed, ie it is still executing but it has finished executing. 执行和执行提供了类似的难题,因为并非所有组合都有意义,例如执行+已执行,即它仍在执行但已完成执行。 But other combinations of these states make sense.
但是这些状态的其他组合是有意义的。 Look at this:
看这个:
|executed: 0 |executed: 1 |
----------------------------------------------------------------------
executing: 0 |not currently executing |executed, finished executing |
executing: 1 |currently executing |executed, still executing ??? |
If you fix the logic of your program, you will find that you don't need both bits. 如果您修改了程序的逻辑,则会发现您不需要这两位。 It is just a sign that there are issues elsewhere.
这仅表明其他地方存在问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.