如何在VB.NET中检查我的布尔变量是否为false

时间:2019-05-30 06:01:33

标签: vb.net

请问这是检查VB.net中变量是否为false的唯一方法吗?

这是我的代码

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

4 个答案:

答案 0 :(得分:2)

更多用于代码审查部分,但我还是给出答案...

出于我的审美观点,写If (a = True) ThenIf (a = False) Then很难看,我宁愿使用If (a) ThenIf (Not a) Then

在您的代码中,如果a + b出错,则不会发生任何事情,因为这种情况不会被处理。 Rolls-Royce实现可能如下所示:

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

作为一般建议,我不会从3个调用中返回布尔值,而是将它们实现为Sub,如果出现问题,则引发异常,这样您就可以提供更准确的反馈,并可以处理更高级别的错误。如果愿意,请调用堆栈。下面是这种实现的示例:

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

答案 1 :(得分:1)

我将解决它,检查每个布尔值,然后存储错误(如果有)。

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

答案 2 :(得分:1)

我的方法是将布尔值放入数组中。通过检查值= False的循环。 False值的索引添加到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

答案 3 :(得分:0)

尽管我喜欢克里斯托弗的回答,但是如果目标是抛出错误消息而不继续,那么您是否也可以这样做?

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