Mr.J
Mr.J

Reputation: 440

How can I check if any of my boolean variable is false in VB.NET

Can I ask if is this is the only way to check if a variable is false in VB.net?

here is my code

if a = true and b = true and c = true then
     msgbox("ok")
else if a= true and b = true and c = false then
     msgbox("c has errors")
else if a= true and b = false and c = true then
     msgbox("b has errors")
else if a= false and b = true and c = true then
     msgbox("a has errors")
end if

Upvotes: 0

Views: 15879

Answers (4)

MikaylaRay44
MikaylaRay44

Reputation: 103

Although I like Cristoph's answer, if the goal is to throw an error message and not proceed then could you not also just do this?

Public Sub DoSomething(a as Boolean, b as Boolean, c as Boolean)

    If Not a Then
        MsgBox("A has errors!") ' Or have a log message or throw an exception.
        Exit Sub                ' Or Return Nothing if it's a function
    End If

    If Not b Then
        MsgBox("B has errors!") ' Or have a log message or throw an exception.
        Exit Sub                ' Or Return Nothing if it's a function
    End If

    If Not c Then
        MsgBox("C has errors!") ' Or have a log message or throw an exception.
        Exit Sub                ' Or Return Nothing if it's a function
    End If

    DoNextThing()

End Sub

Upvotes: 0

Mary
Mary

Reputation: 15091

My approach would be to put the Booleans in an array. The loop through checking for the value = False. The index of the False values are added to lstIndexes.

    Dim lstIndexes As New List(Of Integer)
    Dim bools = {a, b, c}
    For i = 0 To bools.Length - 1
        If Not bools(i) Then
            lstIndexes.Add(i)
        End If
    Next

Upvotes: 1

Mr.Alvaro
Mr.Alvaro

Reputation: 109

I will solve it checking each one of the Boolean then storing the error if any.

Dim Report as String = ""

If a = false Then
    Report = Report & vbCrlf & "a has errors"
End If

If b = false Then
    Report = Report & vbCrlf & "b has errors"
End If

If c = false Then
    Report = Report & vbCrlf & "c has errors"
End If

If Report = "" Then
    Msgbox("OK")
Else
    Msgbox(Report)
End If

Upvotes: 2

Christoph
Christoph

Reputation: 3642

It's more for the code review section, but I give the answers anyway...

For my aesthetics it is ugly to write If (a = True) Then or If (a = False) Then, I would rather use If (a) Then and If (Not a) Then.

In your code, if a + b have errors, nothing happens, as this case is not handled. A Rolls-Royce implementation could look like this:

Public Class Application

    Public Sub DoSomething()
        '... get the results from 3 calls to other methods
        Dim a As Boolean = MethodA()
        Dim b As Boolean = MethodB()
        Dim c As Boolean = MethodC()
        Dim myErrors As String = GetErrorMessage(a, b, c)
        If (myErrors Is Nothing) Then
            MsgBox("ok")
        Else
            MsgBox(myErrors)
            Environment.Exit(-1)
        End If
    End Sub

    Private Function GetErrorMessage(a As Boolean, b As Boolean, c As Boolean) As String
        If (a AndAlso b AndAlso c) Then Return Nothing
        Dim myList As New List(Of String)(3)
        If (Not a) Then myList.Add(NameOf(MethodA))
        If (Not b) Then myList.Add(NameOf(MethodB))
        If (Not c) Then myList.Add(NameOf(MethodC))
        Select Case myList.Count
            Case 1
                Return $"{myList(0)} has errors!"
            Case 2
                Return $"{myList(0)} and {myList(1)} have errors!"
            Case Else
                Return $"{String.Join(", ", myList.Take(myList.Count - 1))} and {myList(myList.Count - 1)} have errros!"
        End Select
    End Function

    Private Function MethodA() As Boolean
        'Does something
        Return True
    End Function

    Private Function MethodB() As Boolean
        'Does something
        Return False
    End Function

    Private Function MethodC() As Boolean
        'Does something
        Return False
    End Function

End Class

As a general recommendation, I would not return a boolean from the 3 calls but implement them as Sub instead and throw an exception if something goes wrong, that allows you to give more accurate feedback and also to handle the error higher up the call stack if you prefere. Here an example of such an implementation:

Public Class Application

    Public Sub DoSomething()
        Dim myErrors As List(Of String) = Nothing
        Try
            MethodA()
        Catch ex As Exception
            If (myErrors Is Nothing) Then myErrors = New List(Of String)(3)
            myErrors.Add($"{NameOf(MethodA)}: {ex.Message}")
        End Try
        Try
            MethodB()
        Catch ex As Exception
            If (myErrors Is Nothing) Then myErrors = New List(Of String)(2)
            myErrors.Add($"{NameOf(MethodB)}: {ex.Message}")
        End Try
        Try
            MethodC()
        Catch ex As Exception
            If (myErrors Is Nothing) Then myErrors = New List(Of String)(1)
            myErrors.Add($"{NameOf(MethodC)}: {ex.Message}")
        End Try
        If (myErrors Is Nothing) Then
            MsgBox("OK")
        Else
            MsgBox($"The following errors occurred:{vbCrLf}{vbCrLf}{String.Join(vbCrLf, myErrors)}")
        End If
    End Sub

    Private Sub MethodA()
        'Does something
    End Sub

    Private Sub MethodB()
        'Does something
        Throw New NotImplementedException()
    End Sub

    Private Sub MethodC()
        'Does something
        Throw New NotSupportedException()
    End Sub

End Class

Upvotes: 4

Related Questions