简体   繁体   English

空值检查

[英]Null value checking

i have one issue 我有一个问题

attrval[5] = WrmService.WindowsAgent.AgentVersion;

From above if attrval[5] is null or not getting any value or any strings other than numeric values i want to assign attrval[5] to value '0.0.0.0' otherwise i will display the numeric value which is coming.What coding i have to implement here 从上面开始,如果attrval [5]为null或没有获取任何值或除数字值以外的任何字符串,我想将attrval [5]分配给值'0.0.0.0',否则我将显示即将到来的数字值。必须在这里实施

and finally at UI there are two possible chances one is value is 0.0.0.0 or numeric value. 最后在用户界面上有两种可能,一种是值是0.0.0.0或数字值。 if it is 0.0.0.0 i will display 'Unknown' string from resource file or i will display the numeric value in LISTVIEW 如果它是0.0.0.0,我将显示资源文件中的“未知”字符串,或者我将在LISTVIEW中显示数字值

i am doing that one like shown below 我正在像下面所示的那样

if(Data.AgentVersion ==null)
                         SubItems.Add(ResourcePolicySystemsLVI.m_nullVersion);
 else
                     SubItems.Add(((IResourcePolicy)Data).AgentVersion);

Is this sufficient means Is 0.0.0.0 is equal to null or i want to change if(Data.AgentVersion ==null) to if(Data.AgentVersion ==0.0.0.0) 这是否足够意味着0.0.0.0等于null还是我想将if(Data.AgentVersion == null)更改为if(Data.AgentVersion == 0.0.0.0)

Comparison with null and comparison with a certain value which represents no value are not the same thing. null进行比较和与表示无值的某个值进行比较不是同一回事。 If that's all you're asking, then you have to check for both separately. 如果仅此而已,则必须分别检查两者。

However I don't know enough about the WrmService to say if a null value is ever possible. 但是我对WrmService了解还不够多, WrmService说明是否可以使用null值。

To answer your basic question 0.0.0.0 is not equivalent to null . 回答您的基本问题0.0.0.0不等于null

Your test should be: 您的测试应为:

if (Data.AgentVersion == null || Data.AgentVersion.Equals("0.0.0.0")

assuming Data.AgentVersion is a string. 假设Data.AgentVersion是一个字符串。

You might want to implement something along the same lines as String.IsNullOrEmpty which you can call where ever you need to do this test. 您可能想要实现与String.IsNullOrEmpty相同的东西,您可以在需要进行此测试的任何地方调用它。

You could try this to check for a null or a number: 您可以尝试执行以下操作以检查是否为空或数字:

attrval[5] = (WrmService.WindowsAgent.AgentVersion == null || Microsoft.VisualBasic.Information.IsNumeric(WrmService.WindowsAgent.AgentVersion)) ? "0.0.0.0" : WrmService.WindowsAgent.AgentVersion;

Or if its just a null check you could try this: 或者,如果只是空检查,则可以尝试以下操作:

attrval[5] = WrmService.WindowsAgent.AgentVersion ?? "0.0.0.0";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM