「VB.NET」Exit SubでIF条件処理を強制終了する

2021年10月22日

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
If not 変数名 Then
Exit Sub
End If
If not 変数名 Then Exit Sub End If
If not 変数名 Then
   Exit Sub
End If

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Dim bRet As Boolean
'データチェック
bRet = CheckData()
'戻り値の判定
If Not bRet Then
   MessageBox.Show("強制終了Exit Sub", "情報表示", MessageBoxButtons.OK)
Exit Sub
End If
MessageBox.Show("test", "情報表示", MessageBoxButtons.OK)
End Sub
Private Function CheckData() As Boolean
Dim res As Boolean = 2 > 4
Return res
End Function
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click Dim bRet As Boolean 'データチェック bRet = CheckData() '戻り値の判定 If Not bRet Then    MessageBox.Show("強制終了Exit Sub", "情報表示", MessageBoxButtons.OK) Exit Sub End If MessageBox.Show("test", "情報表示", MessageBoxButtons.OK) End Sub Private Function CheckData() As Boolean Dim res As Boolean = 2 > 4 Return res End Function
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
    Dim bRet As Boolean

    'データチェック
    bRet = CheckData()

    '戻り値の判定
    If Not bRet Then
           MessageBox.Show("強制終了Exit Sub", "情報表示", MessageBoxButtons.OK)
        Exit Sub
    End If

    MessageBox.Show("test", "情報表示", MessageBoxButtons.OK)
End Sub

Private Function CheckData() As Boolean
    Dim res As Boolean = 2 > 4
    Return res
End Function

実行結果
ボタンを押下すると、「強制終了Exit Sub」というメッセージボックスが表示されます。

VB.net

Posted by arkgame