Sub Sample()
Dim n As Long
n = 12
If Proc1(n) And Proc2(n) Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub
Function Proc1(n As Long)
Debug.Print "Proc1"
If n < 10 Then
Proc1 = True
Else
Proc1 = False
End If
End Function
Function Proc2(n As Long)
Debug.Print "Proc2"
If n Mod 2 = 0 Then
Proc2 = True
Else
Proc2 = False
End If
End Function
Proc1がTrue、かつ、Proc2がTrue、かどうかを判定しています。引数に渡したnは10より大きいので、Proc1の「If n < 10」判定で偽となり、Proc1はFalseを返します。すると「If Proc1(n) And Proc2(n) Then」は、Proc2を評価するまでもなく条件全体が偽と確定します。しかしVB6/VBAでは、このようなケースでも毎回評価する必要のないProc2が評価されていました。イミディエイトウィンドウでご確認ください。これは、Or演算子でも同じことです。
Sub Sample()
Dim n As Long
n = 12
If Proc1(n) AndAlso Proc2(n) Then
MsgBox("Yes")
Else
MsgBox("No")
End If
End Sub
Function Proc1(ByVal n As Long)
Debug.Print("Proc1")
If n < 10 Then
Proc1 = True
Else
Proc1 = False
End If
End Function
Function Proc2(ByVal n As Long)
Debug.Print("Proc2")
If n Mod 2 = 0 Then
Proc2 = True
Else
Proc2 = False
End If
End Function
Sub Sample()
If MessageBox.Show("メッセージ", _
"タイトル", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2, _
MessageBoxOptions.RightAlign, _
True) = Windows.Forms.DialogResult.Yes Then
MsgBox("Yes")
End If
End Sub