PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
checking all arrays-VBForums
Results 1 to 12 of 12

Thread: checking all arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    17

    checking all arrays

    How do I check if all values of an array are false?
    Eg. How would I do this but with less code
    if working(0) = true then
    if working(1) = true then
    if working(2) = true then

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: checking all arrays

    Use this:

    Code:
    For i = 0 to ubound(MyArray)
       If MyArray(i) then
         'true
       Else
         'False
       End if
    Next

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: checking all arrays

    vb Code:
    1. For i = 0 to UBound(MyArray)
    2.    If MyArray(i) Then Exit For
    3. Next i
    4. AllAreFalse = Not MyArray(i)
    5.  
    6. For i = 0 to UBound(MyArray)
    7.    If Not MyArray(i) Then Exit For
    8. Next i
    9. AllAreTrue = MyArray(i)

  4. #4
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: checking all arrays

    Yet another way:

    Code:
    For i = 0 To UBound(Myarray)
        If Myarray(i) Then
            intTrue = intTrue + 1
        Else
            intFalse = intFalse + 1
        End If
    Next
    
    If intTrue = 0 Then
        Debug.Print "All values are false."
    ElseIf intFalse = 0 Then
        Debug.Print "All values are true."
    End If

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    17

    Re: checking all arrays

    Jeroen, your method gives a subscript error.
    Campion, your methods result in alltrue being true even if only one of the variables in the array are true.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    17

    Re: checking all arrays

    This is what i need the code for:
    If notabletofire(1-40) = True Then Call nextlevel

  7. #7
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: checking all arrays

    Quote Originally Posted by Ry12
    This is what i need the code for:
    If notabletofire(1-40) = True Then Call nextlevel
    This helps a little. Otherwise, we're stuck with guessing what you are trying to do.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,226

    Re: checking all arrays

    You shouldn't start your arrays at 1, as it is less efficient than starting them at 0.

    However, this should do what you want:
    Code:
    Dim AnyFalse as Boolean
    For i = 1 To 40
      If notabletofire(i) = False Then 
        AnyFalse = True
        Exit For
      End If
    Next i
    If Not AnyFalse Then Call nextlevel
    Last edited by si_the_geek; Nov 3rd, 2007 at 09:04 AM. Reason: oops..missed the False!

  9. #9
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: checking all arrays

    How about this then?

    Code:
    Public Sub Test()
    
    Dim i As Integer
    Dim NotAbletoFire(10) As Boolean
    
    For i = 0 To UBound(NotAbletoFire)
        If Not NotAbletoFire Then Exit Sub
    Next
    
    Call Nextlevel
    
    End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    17

    Re: checking all arrays

    Neither of those work either. Just to make sure it wasn't a problem with my own code I checked by doing:
    ...
    If NotAbletoFire(34) = True Then
    If NotAbletoFire(35) = True Then
    If NotAbletoFire(36) = True Then
    If NotAbletoFire(37) = True Then
    If NotAbletoFire(38) = True Then
    If NotAbletoFire(39) = True Then
    If NotAbletoFire(40) = True Then
    Call nextlevel

    It worked so I guess your code is still wrong.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,226

    Re: checking all arrays

    I just tested mine, and it works fine - note that there was originally an error in it which I corrected a few minutes later.

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: checking all arrays

    Here ya go
    Code:
    Option Explicit
    
    Private Declare Function memcmp Lib "ntdll.dll" Alias "RtlCompareMemory" ( _
                ByRef Source1 As Any, _
                ByRef Source2 As Any, _
                ByRef Length As Long) As Long
                
    Private Sub Form_Load()
    Dim arr() As Boolean
    Dim arrCopy() As Boolean
    Dim ArrSize As Long
    Dim CmpSize As Long
    
    
        ReDim arr(5)
        ReDim arrCopy(5)
        ArrSize = (UBound(arr) + 1) * LenB(arr(0))
        
        arr(5) = True
        
        CmpSize = memcmp(arr(0), arrCopy(0), ArrSize)
        
        If ArrSize = CmpSize Then
            MsgBox "Arrays are equal"
        Else
            MsgBox "Arrays are not equal"
        End If
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width