枚举组合 - vb.net

时间:2017-03-31 16:36:59

标签: .net vb.net enums

我在Vb.net中有一个项目,并且有一个带有dotnet framework 3.5的windows表单应用程序。我使用Enum来存储和处理任务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

由于 ts 具有当前值:=错误或已执行或已上载或已完成。
由于框架3.5,我无法使用HasFlag

如何比较如果 enum 中包含两个或更多特定值,如代码中所述?

2 个答案:

答案 0 :(得分:4)

要在忽略任何其他标志的同时检查两个标志,可能是:

If (ts And (TaskStatus.uploaded Or TaskStatus.errors)) =
        (TaskStatus.uploaded Or TaskStatus.errors) Then
    ' ...
End If

非常难看。辅助方法可以使事情变得更好:

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

您可以根据需要传入任意数量的标记。

答案 1 :(得分:3)

如果var btnEnter = document.getElementById("entry-btn"); // Uh oh! This is gone! document.getElementById returns null. btnEnter.addEventListener("click", coverFadeOut); // btnEnter is null. I can't add a listener to it! 位为高,则表示已完成。哪个其他位很高并不重要。

您可以完成错误并且值为18(已完成+错误)。它仍然完整,但有错误。

如果你的程序没有使用这个逻辑,那么你正在使用状态位错误。

另外

  • 我不明白为什么你会在你的枚举中跳过1
  • -1代表completed是愚蠢的,因为所有内容都应该是所有枚举值的总和

如果您已经定义了这样的枚举,

all
  • Public Enum TaskStatus completed = 1 executing = 2 executed = 4 errors = 8 uploaded = 16 End Enum 现在为31
  • allincomplete位为0
  • 的时间

以下是如何更好地设计它和有效的使用示例

TaskStatus.Complete

使用测试程序输出:

  

TaskStatus值:18   
没完成   
仍在执行   
完成上传   
  
TaskStatus值:9   
完成有错误   
  
TaskStatus值:已完成   
完成没有错误   
  
TaskStatus值:31   
完成有错误   
你设置所有状态位!

考虑为什么我删除了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 。每个位表示布尔逻辑状态,即它只能是这种或那种方式。如果你有一点完成和不完整,那么你可能会有一个不合逻辑的状态,其中已完成和不完整都是高,值为66 +示例中的任何或所有其他位(incomplete除外) all - 也是我删除这些的原因)。完整和不完整的状态是不确定的,为什么要浪费掉呢?

执行和执行提供类似的难题,因为并非所有组合都有意义,例如执行+执行,即它仍在执行但已完成执行。但这些状态的其他组合是有道理的。看看这个:

none

如果你修复了程序的逻辑,你会发现你不需要这两个位。这只是其他地方存在问题的一个迹象。