VB如果路径感到困惑

时间:2015-11-17 12:39:26

标签: vb.net

我有VB程序,用户可以使用InputBox输入成绩。无论用户输入什么,消息框(msgbox)都在说明"请输入一个数字"出现。如果未输入数字,如何将其更改为仅显示此消息?

Option Strict Off

Public Class Form1

Dim totalpointsaccumultator As Object

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click
    Dim inputProjectPoints, inputTestPoints As String
    Dim grade, projectpoints, testpoints As String
    Dim projectcounter As Integer = 1
    Dim testcounter As Integer = 1
    Dim isconverted As Boolean
    Dim totalpointsaccumulator As Integer
    Do While projectcounter < 5
        inputProjectPoints = InputBox("Enter the points earned on project " & projectcounter, "Grade Calculator", "0")
        inputProjectPoints = projectpoints
        isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))
        If isconverted Then
            totalpointsaccumultator = totalpointsaccumulator + projectpoints
            projectcounter = projectcounter + 1
        Else
            MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    Do While testcounter < 3
        inputTestPoints = InputBox("Enter the points earned on test " & testcounter, "Grade Calculator", "0")
        isconverted = Integer.TryParse(inputTestPoints, testpoints)
        If isconverted Then
            testcounter = testcounter + 1
            totalpointsaccumulator = CInt(totalpointsaccumulator + testpoints)
        Else
            MessageBox.Show("Please enter a number.", "Grade calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    ' assign grade
    Select Case totalpointsaccumulator
        Case Is >= 360
            grade = "A"
        Case Is >= 320
            grade = "B"
        Case Is >= 280
            grade = "C"
        Case Is >= 240
            grade = "D"
        Case Else
            grade = "F"
    End Select
    totalpointsLabel.Text = Convert.ToString(totalpointsaccumulator)
    gradeLabel.Text = grade
End Sub
End Class

1 个答案:

答案 0 :(得分:1)

    isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))

应该是:

    isconverted = Integer.TryParse(inputProjectPoints, projectpoints)

以及:

Dim grade, projectpoints, testpoints As String

应该是:

Dim grade as String
Dim projectpoints, testpoints As Integer

您不能通过尝试转换/转换它来将引用变量作为不同的类型传递,它只返回您请求的类型中的值(如果可能,考虑到您使用的是Integer.TryParse,这是具有讽刺意味的( ))它实际上并没有改变你声明的变量的基础类型。

由于这个问题,你的Integer.TryParse()总是失败,意味着isconverted总是假的,你总是得到消息框。

编辑:忘记添加,Plutonix是对的。设置Option Strict ON !!