Thursday, February 10, 2011

Alternative for Microsoft.VisualBasic.Information.IsNumeric in csharp

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

as per the information while doing googling i did this

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

but Microsoft.VisualBasic.Information.IsNumeric making problems Is there any similar one in Csharp

only two outputs i required one will be numeric and one will be any other, it can be string or null whatever it i have to set in to 0.0.0.0

  • I would recomend using something like

    Int32.TryParse Method (String, Int32)

    See also C# Equivalent of VB's IsNumeric()

    From astander
  • You could use Int32.TryParse() to check if it is an integer value.

    From Andy Rose
  • Try

    if(!int.TryParse(WrmService.WindowsAgent.AgentVersion, out attrval[5])) attrval[5] = 0;
    

    In this case, if AgentVersion is numeric, it will place the parsed value into attrval[5], otherwise it will set it to 0.

    edit

    Ah I guess you are looking for:

    attrval[5] = string.IsNullOrEmpty(WrmService.WindowsAgent.AgentVersion) ? "0.0.0.0" : WrmService.WindowsAgent.AgentVersion;
    
    peter : but its showing error Error 4 ; expected ..If i put in this way if(!int.TryParse(WrmService.WindowsAgent.AgentVersion, out attrval[5])) attrval[5] = "0.0.0.0"; again error
    Jan Jongboom : See edit, I think everyone misinterpreted your question
    peter : thats the right answer

0 comments:

Post a Comment