0

I have a project in Vb.net and windows form application with dotnet framework 3.5. I am using Emum for storing and processing tasks 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

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?

Admin
  • 173
  • 3
  • 16
  • 1
    Possible duplicate of [What does the \[Flags\] Enum Attribute mean in C#?](http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c) – A Friend Mar 31 '17 at 10:32
  • 1
    You probably want to be using [`HasFlag`](https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3). That said, you might want to rethink your structure a little, as right now you could set it to executing _and_ executed - flags only really make sense when things are not mutually exclusive. Several boolean properties might be better here... – James Thorpe Mar 31 '17 at 10:32
  • @JamesThorpe that's programmer's headache to resolve Executed or executing. The main problem is, which one is good? using 'And' or isequalto '=' ? – Admin Mar 31 '17 at 11:08
  • According to MSDN, "The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute..." – dbasnett Apr 01 '17 at 13:15

2 Answers2

2

Here is a class that might help with some ideas. Note the additional enum value all and the flags attribute on the Enum.

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)
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • how to check for two values like 'TaskStatus.uploaded Or TaskStatus.errors' ?? – Admin Mar 31 '17 at 12:24
  • Should i use : If Me.ThisStatus And (TaskStatus.uploaded Or TaskStatus.errors) ?? – Admin Mar 31 '17 at 12:30
  • If Me.ThisStatus = TaskStatus.uploaded Or Me.ThisStatus = TaskStatus.errors – Sasha Mar 31 '17 at 13:01
  • If you have added the other checks it would be if TaskStatus.isUploaded OrElse TaskStatus.isErrors Then... Maybe I wasn't clear about the code not being complete and you would have to complete it. – dbasnett Mar 31 '17 at 22:47
-1

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.

On top of this, I also suggest using elseif instead of normal if statements after each other, just to safe some time and resource :)

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.

Sasha
  • 1,674
  • 1
  • 16
  • 23