结束一个被调用的Sub过程并继续下一个被调用的Sub过程

时间:2014-03-07 04:05:57

标签: vb.net

我目前正在开展一个项目,在该项目中,我使用Validate子过程来验证一组文本框中的一组值,然后使用第二个子过程Sum来添加值的文本框在一起。

目前,当我像这样调用潜艇时:

Private Sub SumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SumButton.Click

    Call ValidateProcedure()
    Call Sum()

End Sub

如果发现错误,ValidateProcedure不会停止,验证程序的工作原理如下:

'目的:确保所有值都输入文本框1-4&类别文本框有效

    Dim Score1Message As String
    Dim Score2Message As String
    Dim Score3Message As String
    Dim Score4Message As String
    Dim CategoryMessage As String

    'TextBox1 Validation
    If Score1TextBox.Text = "" Then
        Score1Message = "Score 1 is blank"
    ElseIf Score1TextBox.Text > 10 Or Score1TextBox.Text < 0 Then
        Score1Message = "Score 1 is not in range: 0-10"
    Else
        Score1Message = "Score 1 is valid"
    End If

    'Textbox 2 Validation 
    If Score2TextBox.Text = "" Then
        Score2Message = "Score 2 is blank"
    ElseIf Score2TextBox.Text > 10 Or Score2TextBox.Text < 0 Then
        Score2Message = "Score 2 is not in range: 0-10"
    Else
        Score2Message = "Score 2 is valid"
    End If

    'Score 3 Validation 
    If Score3TextBox.Text = "" Then
        Score3Message = "Score 3 is blank"
    ElseIf Score3TextBox.Text > 10 Or Score3TextBox.Text < 0 Then
        Score3Message = "Score 3 is not in range 0-10"
    Else
        Score3Message = "Score 3 is Valid"
    End If

    'Score 4 validation 
    If Score4TextBox.Text = "" Then
        Score4Message = "Score 4 is blank"
    ElseIf Score4TextBox.Text > 10 Or Score4TextBox.Text < 0 Then
        Score4Message = "Score 4 is not in range: 0-10"

    Else
        Score4Message = "Score 4 is valid"
    End If

    'Category Validation
    If CategoryTextBox.Text = "" Then
        CategoryMessage = "Category is blank"

    ElseIf CategoryTextBox.Text <> "a".ToUpper Or CategoryTextBox.Text <> "b".ToUpper Or CategoryTextBox.Text <> "c".ToUpper Then
        CategoryMessage = "Category is not value A,B or C"
    Else
        CategoryMessage = "Category is valid"
    End If



    'Display validation strings in statusLabel 
    StatusLabel.Text = Score1Message & Environment.NewLine &
        Score2Message & Environment.NewLine &
        Score3Message & Environment.NewLine &
        Score4Message & Environment.NewLine &
        CategoryMessage






End Sub

所以我要做的是让它验证文本框的值,如果没有错误,那么继续执行Sum子过程。任何建议赞赏!

1 个答案:

答案 0 :(得分:1)

您需要使用Function返回验证结果的布尔值:

If Validate() = True Then
    Sum()
End If

将您的程序(SubValidateProcedure更改为Function

Private Function Validate() As Boolean
    'Because you concatenate all your messages together in the end, 
    'then may be better to use StringBuilder
    Dim errormsg as New StringBuilder()

    'TextBox1 Validation
    If Score1TextBox.Text = "" Then
        errormsg.AppendLine("Score 1 is blank")
    ElseIf Score1TextBox.Text > 10 Or Score1TextBox.Text < 0 Then
        errormsg.AppendLine("Score 1 is not in range: 0-10")
    Else
        errormsg.AppendLine("Score 1 is valid")
    End If

    'Textbox 2 Validation 
    If Score2TextBox.Text = "" Then
        errormsg.AppendLine("Score 2 is blank")
    ElseIf Score2TextBox.Text > 10 Or Score2TextBox.Text < 0 Then
        errormsg.AppendLine("Score 2 is not in range: 0-10")
    Else
        errormsg.AppendLine("Score 2 is valid")
    End If

    'Score 3 Validation 
    If Score3TextBox.Text = "" Then
        errormsg.AppendLine("Score 3 is blank")
    ElseIf Score3TextBox.Text > 10 Or Score3TextBox.Text < 0 Then
        errormsg.AppendLine("Score 3 is not in range 0-10")
    Else
        errormsg.AppendLine("Score 3 is Valid")
    End If

    'Score 4 validation 
    If Score4TextBox.Text = "" Then
        errormsg.AppendLine("Score 4 is blank")
    ElseIf Score4TextBox.Text > 10 Or Score4TextBox.Text < 0 Then
        errormsg.AppendLine("Score 4 is not in range: 0-10")
    Else
        errormsg.AppendLine("Score 4 is valid")
    End If

    'Category Validation
    If CategoryTextBox.Text = "" Then
        error.msg.AppendLine("Category is blank")

    ElseIf CategoryTextBox.Text <> "a".ToUpper Or CategoryTextBox.Text <> "b".ToUpper Or CategoryTextBox.Text <> "c".ToUpper Then
        errormsg.AppendLine("Category is not value A,B or C")
    Else
        errormsg.AppendLine("Category is valid")
    End If

    StatusLabel.Text = errormsg.ToString()
    'Then based on the error messages return a Boolean value:
    If errormsg.Length = 0 Then
        Return True
    Else
        Return False
    End If
End Function