如何使用一行 VB6 退出嵌套子?
How to exit nested sub using one line VB6?
我想知道是否有一种简化的方法可以从另一个嵌套的 Sub
(Sub2) 中退出 Sub
(Sub1);所以,Sub2 是在 Sub1 中调用的,如果在 Sub 中验证失败,我也想退出 Sub1 吗?
Sub Process()
Validate()
'SomeMorecode...
End Sub
Sub Validate()
'...
'...
End Sub
首先,您需要了解订阅没有 return 值。它们就像 C 中的 void
函数。
其次,每当您想退出某个子程序时使用Exit Sub
。
或者,如果您想退出函数,请使用 Exit Function
或者,如果您想退出 Do 循环,Exit Do
Exit While
,等等
切记,如果您在实际 returning 之前执行 Exit Function
,则会自动设置默认值。对于布尔值,该值将为 False。
你明白了
Sub MyFirstSub()
If Validate() Then
'Do more work here
Else
Exit Sub ' Early Exit
End If
'Other things to do after validation is TRUE
'...
'...
End Sub
Function Validate() As Boolean
' Do validation here and either return TRUE or FALSE
If Rnd(1) > 0.5 Then
Validate = True
Else
Validate = False
End If
End Function
我想知道是否有一种简化的方法可以从另一个嵌套的 Sub
(Sub2) 中退出 Sub
(Sub1);所以,Sub2 是在 Sub1 中调用的,如果在 Sub 中验证失败,我也想退出 Sub1 吗?
Sub Process()
Validate()
'SomeMorecode...
End Sub
Sub Validate()
'...
'...
End Sub
首先,您需要了解订阅没有 return 值。它们就像 C 中的 void
函数。
其次,每当您想退出某个子程序时使用Exit Sub
。
或者,如果您想退出函数,请使用 Exit Function
或者,如果您想退出 Do 循环,Exit Do
Exit While
,等等
切记,如果您在实际 returning 之前执行 Exit Function
,则会自动设置默认值。对于布尔值,该值将为 False。
你明白了
Sub MyFirstSub()
If Validate() Then
'Do more work here
Else
Exit Sub ' Early Exit
End If
'Other things to do after validation is TRUE
'...
'...
End Sub
Function Validate() As Boolean
' Do validation here and either return TRUE or FALSE
If Rnd(1) > 0.5 Then
Validate = True
Else
Validate = False
End If
End Function