[英]enum combination, correct way - 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 Emum for storing and processing tasks As : 我正在使用Emum存储和处理任务,如:
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
One function is processing the task and the other one is Checking its completion status, as 一种功能是处理任务,另一种功能是检查其完成状态,如下
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
In the Function CheckStatus, i want to know the correct way to manipulate with enum
combinations? 在功能CheckStatus中,我想知道使用enum
组合进行操作的正确方法吗?
Here is a class that might help with some ideas. 这是一门可以帮助您提出一些想法的课程。 Note the additional enum value all and the flags attribute on the Enum. 注意附加枚举值all和Enum的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
and some usage 和一些用法
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)
I would say that what you have done here is perfectly fine way of doing so, but if you want to check which of the ones are correct where you are using or
, I suggest splitting them up into different if statements. 我会说您在这里所做的工作是一种非常好的方法,但是如果您想检查使用or
正确方法,建议将它们分成不同的if语句。
On top of this, I also suggest using elseif
instead of normal if
statements after each other, just to safe some time and resource :) 最重要的是,我还建议彼此之间使用elseif
代替普通的if
语句,以节省一些时间和资源:)
Here is how I would do it: 这是我的方法:
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
This is just a quick sketch up of how i'd do it, but if I have misread something, let me know. 这只是我做事的简要速览,但是如果我看错了什么,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.