Admin
Admin

Reputation: 193

enum combination, correct way - vb.net

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?

Upvotes: 0

Views: 830

Answers (2)

dbasnett
dbasnett

Reputation: 11773

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)

Upvotes: 2

Sasha
Sasha

Reputation: 1724

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.

Upvotes: 0

Related Questions