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 '' 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 or numeric value. if it is 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)

Is this sufficient means Is is equal to null or i want to change if(Data.AgentVersion ==null) to if(Data.AgentVersion ==

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 is not equivalent to null.

    Your test should be:

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

    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)) ? "" : WrmService.WindowsAgent.AgentVersion;

    Or if its just a null check you could try this:

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

    peter : here what i want is i need only two outputs means it can be or it can be numeric value
    Robert : what about or 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;


Post a Comment