[英]Null value checking
我有一個問題
attrval[5] = WrmService.WindowsAgent.AgentVersion;
從上面開始,如果attrval [5]為null或沒有獲取任何值或除數字值以外的任何字符串,我想將attrval [5]分配給值'0.0.0.0',否則我將顯示即將到來的數字值。必須在這里實施
最后在用戶界面上有兩種可能,一種是值是0.0.0.0或數字值。 如果它是0.0.0.0,我將顯示資源文件中的“未知”字符串,或者我將在LISTVIEW中顯示數字值
我正在像下面所示的那樣
if(Data.AgentVersion ==null)
SubItems.Add(ResourcePolicySystemsLVI.m_nullVersion);
else
SubItems.Add(((IResourcePolicy)Data).AgentVersion);
這是否足夠意味着0.0.0.0等於null還是我想將if(Data.AgentVersion == null)更改為if(Data.AgentVersion == 0.0.0.0)
與null
進行比較和與表示無值的某個值進行比較不是同一回事。 如果僅此而已,則必須分別檢查兩者。
但是我對WrmService
了解還不夠多, WrmService
說明是否可以使用null值。
回答您的基本問題0.0.0.0
不等於null
。
您的測試應為:
if (Data.AgentVersion == null || Data.AgentVersion.Equals("0.0.0.0")
假設Data.AgentVersion
是一個字符串。
您可能想要實現與String.IsNullOrEmpty
相同的東西,您可以在需要進行此測試的任何地方調用它。
您可以嘗試執行以下操作以檢查是否為空或數字:
attrval[5] = (WrmService.WindowsAgent.AgentVersion == null || Microsoft.VisualBasic.Information.IsNumeric(WrmService.WindowsAgent.AgentVersion)) ? "0.0.0.0" : WrmService.WindowsAgent.AgentVersion;
或者,如果只是空檢查,則可以嘗試以下操作:
attrval[5] = WrmService.WindowsAgent.AgentVersion ?? "0.0.0.0";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.