I've seen a code where return is used like below
If myFunction() Then Return
Private Function myFunction() As Boolean
Try //something
return true
Catch
return false
End Try
End Function开发者_运维技巧
I didn't quite get the logic with the "If myFunction() Then return" Any explanation would be much appreciated.
Return
means to exit the function which returns no value. It's equivalent of Exit Sub
in VB6 (if you are familiar with the language).
For example, if I have the following code:
Sub Foo()
If True Then Return
MessageBox.Show("Hello World");
End Sub
The message box will never show because Return
exits the function.
For your case, let's replace If True Then Return
with If myFunction() Then Return
:
Sub Foo()
If myFunction() Then Return
MessageBox.Show("Hello World");
End Sub
Function myFunction As Boolean
Try 'something
Return True
Catch
Return False
End Try
End Function
If myFunction returns true, then the message box will not show. If myFunction fails, then the message box will show.
The If statement works the following way
If 'condition' Then 'do something'
'condition' can either be true or false. If it is true, 'do something' will be executed. myFunction() returns such a true/false value (called Boolean), you can thus write it in the spot of the 'condition'.
If myFunction returns true, then it will return. If myFunction returns false, then it will continue.
I assume in your example that the If statement is out of context (and is actually part of some other Function or subroutine).
精彩评论