If the completed
bit is high, then it is completed. It doesn't matter which other bits are high.
You could complete with errors and have a value of 18 (completed + errors). 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
- I don't see why you would skip 1 in your enum
- -1 to represent
all
is silly because all should be the sum of all your enum values
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
incomplete
is when TaskStatus.Complete
bit is 0
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
not completed
still executing
finished uploading
TaskStatus value: 9
completed with errors
TaskStatus value: completed
completed without errors
TaskStatus value: 31
completed with errors
you set all the status bits!
Think about why I removed incomplete
. Each bit represents a boolean logical state i.e. 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). 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, i.e. 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.