无效的演员表达?

时间:2012-05-29 16:35:24

标签: vb.net

如何从Combobox中的值中删除无效的强制转换表达式以适合examgrade中的等式?

我试图通过在MSDN上查找它来解决它,但是无法解决它。 我尝试使用Examgrade = ().ToString,但这不起作用。

希望你们能指出我正确的方向。仅供参考,这是我的第一个真正的程序,我在C#中成功完成了一次,但删除了源文件,因此这是Visual Basic,并且更容易实现这一点。

星号标记问题行

Public Class Calculator
Dim quarter3 As Integer
Dim quarter4 As Integer
Dim desiredgrade As String
Dim examgrade As String

 Private Sub Button1_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles Button1.Click
    quarter3 = TextBox1.Text
    quarter4 = TextBox2.Text
    desiredgrade = ComboBox1.Text
    ****examgrade = ((desiredgrade - (quarter3 * 0.4) - _
    (quarter4 * 0.4)) / 0.2)****

    If examgrade > 100 Then
        Label5.Text = examgrade + " YOLO"
    ElseIf examgrade < 0 Then
        Label5.Text = "Impossible"
    Else
        Label5.Text = examgrade
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) Handles _
ComboBox1.SelectedIndexChanged
    Dim A As Integer
    Dim B As Integer
    Dim C As Integer
    Dim D As Integer
    A = 90
    B = 80
    C = 70
    D = 60
    ComboBox1.EndUpdate()
End Sub
End Class

2 个答案:

答案 0 :(得分:2)

desiredgrade是一个字符串

examgrade = ((Double.Parse(desiredgrade) - (quarter3 * 0.4) - _
            (quarter4 * 0.4)) / 0.2).ToString()

我正在使用Double。如果需要,您还可以使用Integer

examgrade = ((Integer.Parse(desiredgrade) - (quarter3 * 0.4) - _
            (quarter4 * 0.4)) / 0.2).ToString()

答案 1 :(得分:1)

除非将考试作为字符串进行特定原因,否则请尝试以下操作:

Dim examgrade as Integer

examgrade = ((CInt(desiredgrade) - (quarter3 * 0.4) - _
(quarter4 * 0.4)) / 0.2)

当您将examgrade分配给标签时,VB会自动将examgrade转换为String。