Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 结束被调用的子过程并继续下一个被调用的子过程_Vb.net - Fatal编程技术网

Vb.net 结束被调用的子过程并继续下一个被调用的子过程

Vb.net 结束被调用的子过程并继续下一个被调用的子过程,vb.net,Vb.net,我目前正在从事一个项目,在该项目中,我使用Validate子过程验证一组文本框内的一组值,然后使用第二个子过程Sum将文本框的值添加到一起 目前,当我调用SUB时,如下所示: Private Sub SumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SumButton.Click Call ValidateProcedure() Call Sum() End

我目前正在从事一个项目,在该项目中,我使用
Validate
子过程验证一组文本框内的一组值,然后使用第二个子过程
Sum
将文本框的值添加到一起

目前,当我调用SUB时,如下所示:

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

    Call ValidateProcedure()
    Call Sum()

End Sub
如果发现错误,
ValidateProcedure
不会停止,验证过程如下:

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

    Call ValidateProcedure()
    Call Sum()

End Sub
'目的:确保输入到文本框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
Dim Score1消息作为字符串
Dim Score2消息作为字符串
Dim Score3消息作为字符串
Dim Score4消息作为字符串
Dim CategoryMessage作为字符串
'文本框1验证
如果Score1TextBox.Text=”“,则
Score1Message=“分数1为空”
如果Score1TextBox.Text>10或Score1TextBox.Text<0
Score1Message=“分数1不在范围内:0-10”
其他的
Score1Message=“分数1有效”
如果结束
'文本框2验证
如果Score2TextBox.Text=”“,则
Score2Message=“分数2为空”
如果Score2TextBox.Text>10或Score2TextBox.Text<0
Score2Message=“分数2不在范围内:0-10”
其他的
Score2Message=“分数2有效”
如果结束
“3分验证
如果Score3TextBox.Text=”“,则
Score3Message=“分数3为空”
ElseIf Score3TextBox.Text>10或Score3TextBox.Text<0则
Score3Message=“分数3不在0-10范围内”
其他的
Score3Message=“分数3有效”
如果结束
“4分验证
如果Score4TextBox.Text=”“,则
Score4Message=“分数4为空”
ElseIf Score4TextBox.Text>10或Score4TextBox.Text<0则
Score4Message=“分数4不在范围内:0-10”
其他的
Score4Message=“分数4有效”
如果结束
'类别验证
如果CategoryTextBox.Text=”“,则
CategoryMessage=“类别为空”
ElseIf CategoryTextBox.Text“a”.ToUpper或CategoryTextBox.Text“b”.ToUpper或CategoryTextBox.Text“c”.ToUpper然后
CategoryMessage=“类别不是值A、B或C”
其他的
CategoryMessage=“类别有效”
如果结束
'在statusLabel中显示验证字符串
StatusLabel.Text=Score1Message&Environment.NewLine&
Score2Message&Environment.NewLine&
Score3消息与环境.NewLine&
Score4Message&Environment.NewLine&
分类信息
端接头

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

您需要使用
函数
,该函数返回验证结果的布尔值:

If Validate() = True Then
    Sum()
End If
将您的过程(
Sub
validateProcess
更改为
功能

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
Private Function Validate()作为布尔值
'因为您最终将所有消息连接在一起,
'那么使用StringBuilder可能更好
Dim errormsg作为新的StringBuilder()
'文本框1验证
如果Score1TextBox.Text=”“,则
errormsg.AppendLine(“分数1为空”)
如果Score1TextBox.Text>10或Score1TextBox.Text<0
errormsg.AppendLine(“分数1不在范围内:0-10”)
其他的
errormsg.AppendLine(“分数1有效”)
如果结束
'文本框2验证
如果Score2TextBox.Text=”“,则
errormsg.AppendLine(“分数2为空”)
如果Score2TextBox.Text>10或Score2TextBox.Text<0
errormsg.AppendLine(“分数2不在范围内:0-10”)
其他的
errormsg.AppendLine(“分数2有效”)
如果结束
“3分验证
如果Score3TextBox.Text=”“,则
errormsg.AppendLine(“分数3为空”)
ElseIf Score3TextBox.Text>10或Score3TextBox.Text<0则
errormsg.AppendLine(“分数3不在0-10范围内”)
其他的
errormsg.AppendLine(“分数3有效”)
如果结束
“4分验证
如果Score4TextBox.Text=”“,则
errormsg.AppendLine(“分数4为空”)
ElseIf Score4TextBox.Text>10或Score4TextBox.Text<0则
errormsg.AppendLine(“分数4不在范围内:0-10”)
其他的
errormsg.AppendLine(“分数4有效”)
如果结束
'类别验证
如果CategoryTextBox.Text=”“,则
error.msg.AppendLine(“类别为空”)
ElseIf CategoryTextBox.Text“a”.ToUpper或CategoryTextBox.Text“b”.ToUpper或CategoryTextBox.Text“c”.ToUpper然后
errormsg.AppendLine(“类别不是值A、B或C”)
其他的
errormsg.AppendLine(“类别有效”)
如果结束
StatusLabel.Text=errormsg.ToString()
'然后根据错误消息返回布尔值:
如果errormsg.Length=0,则
返回真值
其他的
返回错误
如果结束
端函数

嘿,法比奥,对不起,我不太懂,我是一个非常业余的程序员。那么我把Validate子过程转换成一个函数?我对功能不太熟悉。例如,知道在何处放置返回值或在本例中括号中包含哪些变量
函数
可以像
子函数一样创建,而无需参数Hanks Fabio,我将完成您编写的代码(有一些事情我还没有遇到,例如Errormsg.appendline和StringBuilder)我不想使用任何我不完全理解的东西,但一旦我完全理解这些概念,我肯定会使用它。谢谢!有没有一种方法可以不用errormsg.length来验证它是真是假?然后只需在所有的
if else
上加上
return false
,然后在code结尾加上
return true
,抱歉打扰了Fabio,但是如果我在所有ElseIf语句的末尾加上return False,那么它将在不传递statuslabel.text的情况下终止!