Kaleon
Kaleon

Reputation: 53

Ending a called Sub procedure and continuing to the next called Sub procedure

I am currently working on a project in which I use a Validate sub procedure to validate a set of values within a group of text boxes and then use a second sub procedure Sum to add the values of the text boxes together.

Currently when I call in the subs like so:

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

    Call ValidateProcedure()
    Call Sum()

End Sub

The ValidateProcedure doesn't stop if an error is found, the validate procedure works like so:

'Purpose: To make sure all values entered into text boxes 1-4 & the category textbox are valid

    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

So what I'm trying to do is make it validate the values of the text boxes and if there are no errors then proceed with the Sum sub procedure. Any advice appreciated!

Upvotes: 1

Views: 152

Answers (1)

Fabio
Fabio

Reputation: 32453

You need to use a Function which return a Boolean value of validating result:

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

Change your procedure(Sub) ValidateProcedure to a 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

Upvotes: 1

Related Questions