3

Possible Duplicate:
Does VB6 short-circuit complex conditions?

I am curious about how IF statements are executed in VB6. For example if I have the statement

If x And y Then
    'execute some code
End If

Does the code move on if x is not true? Or does it go ahead and evaluate y even though there is no logical point?

Another example

If x Or y Then
    'execute some code
End If

Does the code continue and evaluate y if x is true?

EDIT: Is there a way to avoid nested IF statements if I want to evaluate very complex conditions and I don't want to waste CPU time?

4

3 回答 3

4

What you are describing is short circuiting logic, and VB6 doesn't have it...

For example, in VB.Net you might write

If x AndAlso y then...

In this case y is not tested if x turns out to be false.

In your VB6 example, you'll get a Object or With block variable not set error if you try something such as:

Dim x as Object
If Not x Is Nothing And x.y=1 Then

Since object x has not been instantiated.

于 2012-10-15T15:16:41.060 回答
3

An unwieldy or-like statement that exhibits short circuiting behaviour:

select case True
   case a(), b(), c()
      '//if a returns true b & c are not invoked, if b returns true a & b were invoked
   case else
      ...
于 2012-10-15T15:46:16.407 回答
2

To answer your edit - avoiding nested IF statements, you can use Select Case, covered in the latter half of this article.

Code snippet from the article:

Select Case strShiftCode
   Case "1" 
      sngShiftRate = sngHourlyRate
   Case "2" 
      sngShiftRate = sngHourlyRate * 1.1
   Case "3"
      sngShiftRate = sngHourlyRate * 1.5
   Case Else
      Print "Shift Code Error"
End Select
于 2012-10-15T15:40:40.010 回答