Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i was suprised when value "NaN" comming into check of below code was recognized as correct value for double parse :O and went into if and nto else... Do you know why is that and how to secure my code better to avoid such situation. Value should go to if only if number can be converted to double.

 If Double.TryParse(array(3), doubleitem) Then

                array(3) = doubleitem
            Else
                array(3) = Nothing
            End If

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is not a misbehavior of Double.TryParse, but a Misunderstanding on your side.
The Double structure actually contains a constant called NaN, that represents a value that is not a number. So when you do a Double.TryParse on the string "NaN", your result is Double.NaN which is a valid value for double.

Change your condition to this

If Double.TryParse(array(3), doubleitem) andalso not Double.IsNaN(doubleitem) Then
     array(3) = doubleitem
Else
     array(3) = Nothing
End If

and everything should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...