退出子功能的最佳做法

时间:2018-07-31 15:02:32

标签: vb.net winforms function return

我正在寻找一种编写将停止所有代码(退出子代码)的函数的方法,而无需在子例程中为此函数编写 If语句

例如:

Public Class Form_Main
    Private Sub Button_Search_Click(sender As Object, e As EventArgs) Handles Button_Search.Click
        If CountNumbers() = False Then Exit Sub 
        ' Is there a way for me to write "CountNumbers" only and it will
        ' [Exit Sub] without me having to write an if statement?
        ' Sort of embedding the Exit Sub in the function?
    End Sub

    Function CountNumbers()
        Dim a As Integer = 1
        If a = 1 Then
            Return False
        End If
        Return True
    End Function

End Class

1 个答案:

答案 0 :(得分:0)

您使用Return关键字而没有提供值

Public Sub Foo()
    If Bar() = False Then
        Return
    End If
End Sub