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

StackOverflow https://stackoverflow.com/questions/8874653

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

You could use Case statements. It's a little bit cleaner than ifs: http://msdn.microsoft.com/en-us/library/cy37t14y%28v=vs.80%29.aspx

Dim Result as boolean
result = false
If condition1 Then result = true
ElseIf condition2 Then result = true
ElseIf condition3 Then result = true
ElseIf condition4 Then result = true

If result Then debug.print "Success"

If you wanted to use a select statement where conditions are not the same then use:

Select Case True
    Case A=5,b=10,c="my answer",d=11
      ....
End Select
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top