代碼的大范圍顯示了 VB6 應用程式中的 .net 表單。我遇到的小問題是為表單設定兩個屬性。此邏輯適用于另一種形式,但由于某種原因,我無法在第二種形式上設定設備(輸入引數 1)或掃描儀(輸入引數 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?
uj5u.com熱心網友回復:
字串變數 Device 不等于 Nothing,它被初始化為 String.Empty。那不一樣。
嘗試更改If陳述句以測驗空字串,例如
If Device.Length = 0 Then
或者
If Device.Equals(String.Empty) Then
uj5u.com熱心網友回復:
對于字串,您可以使用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應該開啟!
當你設定這個選項時,你會發現一堆新的編譯器錯誤,并且很想把它關掉。不要那樣做!這些錯誤中的每一個都是您的程式在運行時有可能失敗的地方,而且它們幾乎都可以輕松修復。花時間進行這些調整,您的程式將更加穩定和快速。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399291.html
上一篇:是否仍然沒有簡單的方法將帶有復合表情符號的字串拆分為陣列?
下一篇:多次出現同一字符的鏈式替換
