Search code examples
if-statementvb6limitcontinuations

Best practice for 'if' statements that exceed the 10 line continuation limit in Visual Basic 6.0


Example:

If condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or _
   condition or Then
    Do something
End If

Say I have more than these 10 conditions I need to evaluate... Is there a better way than just nesting multiple sets of these if statements?


Solution

  • Here's an option -- do one test at a time, tracking the final result in a boolean. When you're all done, just test the boolean.

    Dim A As Long
    Dim B As Long
    Dim C As Long
    Dim D As Long
    
    Dim Result As Boolean
    
    Result = True
    Result = Result And (A > 10)
    Result = Result And (B > 10)
    Result = Result And (C > 10)
    Result = Result And (D > 10)
    
    If Result Then
        ' Do "something" here...
    End If
    

    If any of A, B, C, or D is less than 10, Result will flip to False and stay that way from then on. It will only be True if all of the tests pass.