logical ANDing a boolean?

I want to do this:

sub procA()
   dim success as boolean = true
   for i=1 to 3
      success &= procB(i)
   next
   if success then
   '...
end sub

function procB(iVal as integer) as boolean
   '...
   <return true/false>
end function

Basically, I want to logically AND all the tests in procB. If any are false, then success should be false.

I tried this, ugly as it is:
   for i=1 to 3
      if (!(
procB(i))) then
      success = false
   next


but I can't even do that - the '!' gives me an 'identifier expected' error. So now I'm stuck with the ugly:

   for i=1 to 3
      if ((procB(i)) = false) then
      success = false
   next


I know this is a basic technique, it just escapes me.




Answer this question

logical ANDing a boolean?

  • Ali Gullen

    Or without even using bool :)

    Sub procA()

     Dim i As Integer

     For i = 1 To 3
      If Not procB(i) Then
       Exit For
      End If
     Next

     If i = 4 Then
      ''** Everything succeded
     Else
      ''** Something failed, and i is what failed
     End If

    End Sub



  • HighDesert_NM

    Something to help, boolean operation comparision

    C

    a = b & c
    a = b | c
    a = b ^ c
    a = !b

    VB

    a = b AND c
    a = b OR c
    a = b XOR c
    a = NOT b

    ===============

    so you could do

    sub procA()
       dim success as boolean = true
       for i=1 to 3
          success = success AND procB(i)
       next
       if success then
       '...
    end sub



  • madhusrp

    Not in VB.NET

  • Ahmed_khemiri

    *Slaps forehead*

    That's what I get for switching back & forth. I was doing JavaScript yesterday, which uses !.

    Thanks.

  • Alfy

    OK, so I've got this:

    dim success as boolean  = true
    success = success AND proc(i)

    That way, if any process fails, success will be false.


    Question:

    Can 'success = success AND proc(i)' be shortened any further Such as 'success &=  proc(i)' - (which isn't the right syntax)





  • Chequer

    The ! is a C# construct, the VB equivalent is Not:

    If Not ProcB(i) Then success = False


    Unless ProcB has some side effects, you can exit the for loop when you find the first false value.

    Sub ProcA
       Dim success As Boolean = True
       For i As Integer = 1 To 3
           If Not ProcB(i) Then
               Success = False
               Exit For
           End If
       Next
    End Sub

  • Ray Angeles

     Anarchy wrote:
    because TRUE is -1 and FALSE is 0, if you multiply them all together, then if any of the booleans you are testing is false(0), then the return value will be 0


    No!  True is True and False is False.  In my opinion, using a boolean value as an integer is inappropriate.


            Dim bln1 As Boolean = True
            Dim bln2 As Boolean = True
            Dim bln3 As Boolean = True

            If CInt(bln1) * CInt(bln2) * CInt(bln3) = CInt(True) Then
                MsgBox("true")
            Else
                MsgBox("false")
            End If


    Why incur the cost of an unnecessary conversion   How is this any simpler than this

            Dim bln1 As Boolean = True
            Dim bln2 As Boolean = True
            Dim bln3 As Boolean = True

            If bln1 AndAlso bln2 AndAlso bln3 Then
                MsgBox("true")
            Else
                MsgBox("false")
            End If


  • RJ

    because TRUE is -1 and FALSE is 0, if you multiply them all together, then if any of the booleans you are testing is false(0), then the return value will be 0

            Dim bln1 As Boolean = True
            Dim bln2 As Boolean = True
            Dim bln3 As Boolean = True

            If CInt(bln1) * CInt(bln2) * CInt(bln3) = CInt(True) Then
                MsgBox("true")
            Else
                MsgBox("false")
            End If



  • logical ANDing a boolean?