【问题标题】:InvalidCastException was unhandled?InvalidCastException 未处理?
【发布时间】:2012-04-18 05:05:42
【问题描述】:
    Dim CustID As String = txtSrchCustID.Text
    Dim FirstName As String = txtNewCustFName.Text
    Dim SecondName As String = txtNewCustSName.Text

    If CustID And FirstName And SecondName = "" Then
        MsgBox("Please enter a term to search by")
    EndIf

这将返回“从字符串“转换为类型 'Long' 无效。”我想知道错误是什么以及如何修复它?我看过的其他问题主要与分配不正确类型的变量有关,但我认为这不是问题。它发生在所有变量都为空时。

谢谢!

【问题讨论】:

  • 将 Option Strict On 放在代码顶部或在项目属性中设置它,它将帮助您捕获这些类型的错误。
  • 我已经做到了,谢谢:)

标签: vb.net exception casting


【解决方案1】:

你想做什么。你想检查 所有 他们是""。然后这样做:

If string.isNullOrEmpty(CustID) and _  
    string.isNullOrEmpty(FirstName) And string.isNullOrEmpty(SecondName) Then
        MsgBox("Please enter a term to search by")
    End If

或者您是否要检查其中 一个 是否为""。然后这样做:

If string.isNullOrEmpty(CustID) orelse _  
    string.isNullOrEmpty(FirstName) orelse string.isNullOrEmpty(SecondName) Then
        MsgBox("Please enter a term to search by")
    End If

【讨论】:

  • 没问题.. @user1305482:如果您认为答案很好,记得点赞。它给了我们所有温暖模糊的感觉:P
  • 好消息!我现在有足够的声望给你一个赞成票! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多