【问题标题】:System.InvalidCastException 'Conversion from string "" to type 'double' is not valid'System.InvalidCastException '从字符串 "" 到类型 'double' 的转换无效'
【发布时间】:2020-02-05 12:20:17
【问题描述】:

我一直在为我的大学做一个项目,我必须为服务员创建一个可以在手持计算机上使用的程序。我创建了另一个表单,用户可以在其中输入表号,但是当输入到文本框为空后会发生错误。

System.InvalidCastException '从字符串 "" 到类型 'double' 的转换无效'

Public Module GlobalVariables
    Public TableNumber As Integer
End Module

Public Class Form1


    Private Sub TableNumber_TableNumber_TextChanged(sender As Object, e As EventArgs) Handles TableNumber_TableNumber.TextChanged
        If TableNumber_TableNumber.Text > 25 Then
            TableNumber_Error.Visible = True
            If TableNumber_TableNumber.Text < 1 Then
                TableNumber_Error.Visible = True
            End If
        End If

    End Sub
End Class

这是我第一次在 Visual Basic 中编码,我很困惑,因为我没有将任何东西转换为双精度。

【问题讨论】:

  • 您需要做的是设置Option Strict On,让Visual Studio 向您展示问题并提出解决方案。
  • 然后决定当TableNumber_TableNumber TextBox 中没有任何内容时你想要发生什么。您可以使用Int32.TryParse 来检查输入的内容是否实际上是一个数字,而不是像“是”这样的东西。
  • 我有 Option Strict,在我设计代码时它没有任何建议。
  • 如果 Option Strict 为 On,则表明 If TableNumber_TableNumber.Text &gt; 25 Then 行有问题。为了更加确定,您可以在所有代码之前的第一行输入Option Strict On
  • 当然你没有有 Option Strict On。 TableNumber_TableNumber.Text &gt; 25 将字符串与数值进行比较,而 Option Strict On 则无法做到这一点。此外,当您处理 TextChanged 事件时,您会检查 Text 属性是否返回一个空/空字符串(例如,if string.IsNullOrEmpty(TableNumber_TableNumber.Text) then returnIsNullOrWhitespace 等作为最小验证)

标签: vb.net


【解决方案1】:

我根据您正在执行的逻辑编写了一个类似的 Sub。我在可能有帮助的代码旁边添加了一些注释。

Private Sub txtTest_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtTest.TextChanged

        ' Make sure textbox isn't empty.  Really you'd have front-end validation to handle this, but never hurts to add server-side
        ' I like to use the 'Me' keyword -- just a habit, but you don't have to use it
        If Not String.IsNullOrWhiteSpace(Me.txtTest.Text) Then

            ' 'R' is used to declare a double
            ' If you want to it be an Integer, then update the code accordingly
            Dim numberToCompare As Double = 0R

            ' trim the text to remove any leading/trailing spaces
            If Not Double.TryParse(Me.txtTest.Text.Trim(), numberToCompare) Then

                ' the conversion failed...user might have entered a letter or symbol
                ' display error message if desired, and exit the sub
                Return
            End If


            ' If we got here, the Parse succeeded
            If numberToCompare > 25.0R Then

                ' execute logic

            ElseIf numberToCompare < 1.0R Then

                ' execute logic
                ' if it passed the first logic check, then it won't execute this ElseIf
            Else

                ' none of the above logic checks were true
                ' execute logic

            End If


        Else
            ' display error message if desired
        End If

    End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    相关资源
    最近更新 更多