2

I am curious if it is possible to parse an input box for a data type and if it does not match the data type it will loop until the correct type is done. I understand how to do this with ranges but would rather not if it is possible.

The code I have is:

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

I know that GoTo isn't a "right" or preferred solution but I used that more as a temporary place holder. Any help would be appreciated as this is beyond my programming abilities currently.

4

3 回答 3

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
于 2013-08-11T03:54:17.480 回答
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...")
于 2013-08-11T00:57:49.220 回答
0

首先,On Error GoTo用 Try-Catch 语句替换你拥有的每一个语句,或者至少使用一个函数而不是 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).

Mark Hall,你不认为将你提供的整个代码块变成一个函数会更好吗?就像我一样。这将节省一些代码行。

于 2013-08-11T03:28:26.740 回答