视觉基本功能返回退出子

1 vb.net

我正在为朋友创建一个 Visual Basic 应用程序。无论如何,我正在尝试创建一个函数,该函数将“exit sub”返回给调用该函数的子函数。我已经看到了一些解决这个问题的方法,例如返回 1 或 2 这样的值并在调用 sub 时插入 if 。只是想知道是否有一个我还没有学过的返回退出子的简写。

Private Sub Button1.click() Handles Button1.Click

tryactive()

endsub    
Private Function tryactive()
        Try
            AppActivate("your aplication")
        Catch ex As Exception
            Dim msgboxresponse = MsgBox("please start your application", 0, "Can't find your application")
            If msgboxresponse = MsgBoxResult.Ok Then
                Exit Sub <------ this is the problem i want to send this back to calling sub
            End If
        End Try

    End Function
Run Code Online (Sandbox Code Playgroud)

代码更大,按钮也更多。这就是为什么我问是否有更好的方法来做到这一点。任何帮助表示赞赏。

All*_*uya 5

首先,您不能在Function内使用Exit Sub。它应该是一个退出函数。但根据你想要发生的事情(我猜),尝试一下这个。

Private Sub Button1_Click() Handles Button1.Click

    If TryActive() = False Then
       Exit Sub
    End If

    'Your code you want to execute if TryActive() is True

End Sub

Private Function TryActive() as Boolean
    Try
        AppActivate("your aplication")
        Return True
    Catch ex As Exception
        Dim msgboxresponse = MsgBox("please start your application", 0, "Can't find your application")
        If msgboxresponse = MsgBoxResult.Ok Then
            Return False
        End If
    End Try
End Function
Run Code Online (Sandbox Code Playgroud)