【问题标题】:String equals nothing but also does not equal nothing. Variable cannot be set字符串不等于什么,但也不等于什么。无法设置变量
【发布时间】:2021-12-29 15:53:50
【问题描述】:

代码的大范围是在 VB6 应用程序中显示 .net 表单。我遇到的小问题是为表单设置两个属性。此逻辑适用于另一种形式,但由于某种原因,我无法在第二种形式上设置 Device(输入参数 1)或 Scanner(输入参数 2)。

       'working case statement
       Case ScannerEdit
            Dim Device As String = String.Empty
            Dim Scanner As Integer = 0

            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
                Device = formInputParameters(0)
            End If
            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 2 Then
                Scanner = formInputParameters(1)
            End If

            Dim f As frmScannerEdit = base
            f._Device = Device
            f._Scanner = Scanner

        'not working case statement
        Case ScannerCommandsList

            Dim Device As String = String.Empty
            Dim Scanner As Integer = 0

            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
                Device = formInputParameters(0)
            End If
            If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 2 Then
                Scanner = formInputParameters(1)
            End If

            Dim f As frmScannerCommandsList = base
            f._Device = Device
            f._Scanner = Scanner

在调试中运行时,代码会创建 Device 并将其设置为 Nothing,然后当参数集出现而不是将其设置为“TestDevice”时,它将保持为空。与扫描仪相同。

我试过了

If Device = Nothing Then
    Device = "TestDevice"
End If

在 set Device if 语句中,但它不认为这是真的;即使 Device = Nothing,该声明的结论也是错误的。为什么我不能设置变量,为什么变量不等于Nothing时变量不等于Nothing?

【问题讨论】:

  • 这不是VB6,它是vb.net。这是两种不同但相似的语言。请适当地编辑您的帖子和标签。
  • formInputParameters 是如何定义的。我不确定问题是什么?这两个 case 语句是否在同一个方法中?如果是这样,您是说使用完全相同的 formInputParameters 调用一个有效,一个无效?
  • 会不会是Device在代码中被多次声明,在不同的范围内?
  • If Device Is Nothing Then

标签: string vb.net parameters integer


【解决方案1】:

字符串变量Device不等于Nothing,它被初始化为String.Empty。那是不一样的。

尝试更改If 语句以测试空字符串,例如

If Device.Length = 0 Then

If Device.Equals(String.Empty) Then

【讨论】:

    【解决方案2】:

    对于字符串,您可以使用String.IsNullOrEmpty()String.IsNullOrWhitespace() 进行此类检查。

    此外,可以使用实际元素创建一个数组(该数组不是 Nothing 并且具有所需的长度),但仍将 Nothing 值分配给数组元素之一。例如,给定这个数组和问题中完全相同的代码Device 肯定会以Nothing 结束:

    Dim formInputParameters() As String = {Nothing, "0"}
    
    If formInputParameters IsNot Nothing AndAlso formInputParameters.Length >= 1 Then
        Device = formInputParameters(0)
    End If
    

    最后,我看到Scanner 被声明为一个整数,而Device 是一个字符串,但两者都是从同一个数组中赋值的(类型未知,因为我们看不到它的声明位置)。 那不行! 这表明你没有使用Option Strict。现在已经不是 1997 年了。 Option Strict 应该打开!

    当您设置此选项时,您会发现一堆新的编译器错误,并且很想将其关闭。不要那样做!这些错误中的每一个都是您的程序在运行时可能会失败的地方,并且它们几乎都可以轻松修复。花点时间进行这些调整,您的程序将会更加稳定和快速。

    【讨论】:

      【解决方案3】:

      解决方法是更改​​第二个 case 语句中的变量名称,因为不知何故第一个声明它们并使其什么都没有。我假设这是一个 VB 怪癖。

      【讨论】:

        猜你喜欢
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 2012-02-15
        • 2018-03-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        相关资源
        最近更新 更多