Thursday, April 21, 2011

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

and finally at UI there are two possible chances one is value is 0.0.0.0 or numeric value. if it is 0.0.0.0 i will display 'Unknown' string from resource file or i will display the numeric value in 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)

From stackoverflow
  • Comparison with null and comparison with a certain value which represents no value are not the same thing. 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.

  • To answer your basic question 0.0.0.0 is not equivalent to null.

    Your test should be:

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

    assuming Data.AgentVersion is a string.

    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.

    peter : this will be answer for second ,,but for first one what to do for attrval[5]
    ChrisF : @peter - I'm not sure I follow. @Robert's answer seems good for that. If not then can you update your question with more information.
  • 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";

    peter : here what i want is i need only two outputs means it can be 0.0.0.0 or it can be numeric value
    Robert : what about 1.1.0.1 or 0.1.2.3 are these acceptable values
    peter : Microsoft.VisualBasic.Information for this which namespace i want to use
    peter : its in csharp not visual basic
    Robert : You can use VB libraries in C#, you only need to worry about them if you intend to run your app on another framework like mono. If you are only ever going to use MS .Net then the VB libraries are included.
    peter : but making problems in terms of namespace
    Robert : what problems, you can just place a using derective: using Microsoft.VisualBasic;

0 comments:

Post a Comment