我很好奇是否可以解析数据类型的输入框,如果它与数据类型不匹配,它将循环直到完成正确的类型。我理解如何使用范围来做这件事,但如果可能的话,我宁愿不这样做。
我的代码是:
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim amountAssignments As Integer
Dim pointsEarned As Integer = 0
Dim pointsEarnedTotal As Integer = 0
Dim pointsPossible As Integer = 0
Dim pointsPossibleTotal As Integer = 0
Dim Assignment As Integer = 1
Integer.TryParse(txtAmount.Text, amountAssignments)
Do Until Assignment > amountAssignments
txtAmount.Text = String.Empty
pointsEarned = Integer.Parse(InputBox("Please enter the amount, as a whole number, of Points Earned for Assignment " & Assignment & ":"))
On Error GoTo Check
pointsEarnedTotal = pointsEarnedTotal + pointsEarned
pointsPossible = Integer.Parse(InputBox("Please enter the amount, as a whole number, of Points Possible for Assignment " & Assignment & ":"))
On Error GoTo Check
pointsPossibleTotal = pointsPossibleTotal + pointsPossible
Assignment = Assignment + 1
Loop
lblGrade.Text = (pointsEarnedTotal / pointsPossibleTotal)
Check:
MessageBox.Show("An error has occured, most likely due to an improper value in the points earned or possible box. Please try running the program again with proper values.", "Please run the program again", _
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub
我知道GoTo不是一个“正确”或首选的解决方案,但我更多地将其用作临时占位符。任何帮助都将受到赞赏,因为这超出了我目前的编程能力。
答案 0 :(得分:2)
我会考虑使用Integer.TryParse进行所有转换而不是Parse,这样可以让您测试转换是否失败而不抛出错误。这样的事情应该有效
If Integer.TryParse(txtAmount.Text, amountAssignments) Then
Do Until Assignment > amountAssignments
txtAmount.Text = String.Empty
If Not Integer.TryParse(InputBox("Please enter the amount, as a whole number, of Points Earned for Assignment " & Assignment & ":"), pointsEarned) Then
showError()
Exit Sub
End If
pointsEarnedTotal = pointsEarnedTotal + pointsEarned
If Not Integer.TryParse(InputBox("Please enter the amount, as a whole number, of Points Possible for Assignment " & Assignment & ":"), pointsPossible) Then
showError()
Exit Sub
End If
pointsPossibleTotal = pointsPossibleTotal + pointsPossible
Assignment = Assignment + 1
Loop
lblGrade.Text = (pointsEarnedTotal / pointsPossibleTotal)
Else
showError()
End If
将消息框放在像这样的子程序中。
Sub showError()
MessageBox.Show("An error has occured, most likely due to an improper value in the points earned or possible box. Please try running the program again with proper values.", "Please run the program again", _
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub
答案 1 :(得分:1)
我很好奇是否可以解析数据类型的输入框 如果它与数据类型不匹配,它将循环直到正确 类型已完成。
如果Integer.TryParse失败,请调用自行重试的函数。
Private Function AskInteger(prompt As String) As Integer
Dim result As Integer
If Not Integer.TryParse(InputBox(prompt), result) Then
MessageBox.Show("An error has occurred...")
Return AskInteger(prompt)
End If
Return result
End Function
这将继续,直到解析成功并最终返回整数,因此可以安全地调用它而不进行错误检查:
pointsEarned = AskInteger("Please enter the amount...")
答案 2 :(得分:0)
首先,用Try-Catch语句替换你拥有的每一个On Error GoTo
,或者至少使用一个函数代替GoTo!完全使用GoTo是一种非常糟糕的做法。这将是一个巨大的痛苦,尤其是当你回来更新某些内容或者如果其他人试图阅读代码时会非常困难。请参阅:http://forums.devshed.com/showpost.php?p=2605339&postcount=3和http://www.drdobbs.com/jvm/programming-with-reason-why-is-goto-bad/228200966。
这是nmclean答案的一点增强版本
Private Function GetInput(ByVal prompt As String, ByVal NumOfRetries As Integer) As Integer
Try
Return Integer.Parse(InputBox(prompt))
Catch ex As Exception
If NumOfRetries > 0 Then
NumOfRetries -= 1
Return GetInput(prompt, NumOfRetries)
Else
Return Nothing
End If
End Try
End Function
当然,要运行此功能,请使用Dim theInt As Integer = GetInput("123", 5)
。