我在Vb.net中有一个项目,并且有一个带有dotnet framework 3.5的windows表单应用程序。我正在使用Emum存储和处理任务As:
Public Enum TaskStatus
none = 0
completed = 2
executing = 4
executed = 8
errors = 16 '' Means task got some error in performing some operations
uploaded = 32
incomplete = 64 '' Means Task Was Aborted or Process Stopped in between
End Enum
一个功能是处理任务,另一个是检查其完成状态,如
Private Function Manage()
Dim ts As TaskStatus = TaskStatus.none
'' Performing Tasks
ts = TaskStatus.executing
'' Task Performed with Error
ts = TaskStatus.errors Or TaskStatus.executed
'' Task Uploading
ts = ts Or TaskStatus.uploaded
ts = ts Or TaskStatus.completed
ts = TaskStatus.none
CheckStatus(ts)
End Function
Private Function CheckStatus(ByVal ts As TaskStatus)
' Now i Want to check
If ts And (TaskStatus.uploaded Or TaskStatus.errors) Then
'' Which one of these(Below) is Correct
End If
If ts = (TaskStatus.uploaded Or TaskStatus.errors) Then
'' Which one of these(Above one) is Correct
End If
If ts And TaskStatus.incomplete Then
'' Is it Correct way to check for incompletion
End If
If ts And TaskStatus.completed Then
'' Task is Completed
'' Is is correct way to check Task Completed
End If
End Function
在Function CheckStatus中,我想知道使用enum
组合进行操作的正确方法吗?
答案 0 :(得分:1)
这是一个可能有助于一些想法的课程。请注意额外的枚举值all和枚举上的flags属性。
Public Class StatusTask
<Flags> _
Public Enum TaskStatus
none = 0
completed = 2
executing = 4
executed = 8
errors = 16 '' Means task got some error in performing some operations
uploaded = 32
incomplete = 64 '' Means Task Was Aborted or Process Stopped in between
all = -1
End Enum
Public ThisStatus As TaskStatus = TaskStatus.none
Public Sub SetStatus(aStatus As TaskStatus)
Me.ThisStatus = aStatus
End Sub
Public Sub AddStatus(aStatus As TaskStatus)
Me.ThisStatus = Me.ThisStatus Or aStatus
End Sub
Public Sub ClearStatus(aStatus As TaskStatus)
Me.ThisStatus = Me.ThisStatus And (aStatus Xor TaskStatus.all)
End Sub
Public Function HasStatus(aStatus As TaskStatus) As Boolean
''if HasFlag not found use
''Return (Me.ThisStatus And aStatus) = aStatus
Return Me.ThisStatus.HasFlag(aStatus)
End Function
End Class
和一些用法
Dim foo As New StatusTask
'set some status
foo.SetStatus(StatusTask.TaskStatus.completed)
foo.AddStatus(StatusTask.TaskStatus.executed)
foo.AddStatus(StatusTask.TaskStatus.incomplete Or StatusTask.TaskStatus.uploaded)
Debug.WriteLine(foo.ThisStatus)
'do some checks
'single
If foo.HasStatus(StatusTask.TaskStatus.completed) Then
Stop
End If
'multiple(both must be set)
If foo.HasStatus(StatusTask.TaskStatus.completed) AndAlso foo.HasStatus(StatusTask.TaskStatus.incomplete) Then
Stop
End If
'multiple(both must be set) alternative
If foo.HasStatus(StatusTask.TaskStatus.completed Or StatusTask.TaskStatus.executed) Then
Stop
End If
'multiple(either set)
If foo.HasStatus(StatusTask.TaskStatus.errors) OrElse foo.HasStatus(StatusTask.TaskStatus.incomplete) Then
Stop
End If
'clear status
foo.ClearStatus(StatusTask.TaskStatus.errors) 'errors not set
Debug.WriteLine(foo.ThisStatus)
foo.ClearStatus(StatusTask.TaskStatus.completed Or StatusTask.TaskStatus.uploaded)
Debug.WriteLine(foo.ThisStatus)
foo.ClearStatus(StatusTask.TaskStatus.incomplete)
Debug.WriteLine(foo.ThisStatus)
foo.ClearStatus(StatusTask.TaskStatus.all)
Debug.WriteLine(foo.ThisStatus)
答案 1 :(得分:-1)
我会说你在这里所做的是非常好的方式,但如果你想检查哪一个是正确的,你使用or
,我建议将它们分成不同的语句。
除此之外,我还建议使用elseif
代替正常的if
语句,以保证一些时间和资源:)
我将如何做到这一点:
Private Function CheckStatus(ByVal ts As TaskStatus)
' Now i Want to check
If ts = TaskStatus.uploaded Then
'Uploaded
Elseif ts = TaskStatus.errors Then
'Error
Elseif ts = TaskStatus.incomplete Then
'Incomplete
Elseif ts = TaskStatus.completed Then
' Task is Completed
End If
End Function
这只是我如何做到的快速草图,但如果我误读了某些内容,请告诉我。