0

In one of my program's validation, I need to check if a resulting number stored in a session is undefined (this is due to one of the formula may have a dividend of zero).

What I tried so far (and it may be a very inefficient way of doing it) is this:

  Dim x As Double
  Dim valid As Boolean = True
  Try
      Double.TryParse(Session("result"), x)
  Catch ex As Exception
      valid = False   
  End Try

I figured that if the number stored in the session is indefinite or undefined, the TryParse function will fail. What do you think is the better way to catch undefined numbers stored in object?

P.S. Unfortunately, I cannot validate the function where the Session("result") will originate. This is because another module created by another coder is just passing that to the module I'm coding.

lulutanseco
  • 313
  • 1
  • 8
  • 29
  • `Dim valid As Boolean = Double.TryParse(Session("result"), x)`. `Session("result")` returns `Nothing` if `result` is not defined – Slai Sep 29 '16 at 03:48
  • Hello! the problem is not the Session returning nothing. The session is resulting to a mathematically undefined or indefinite number (which results from dividing a number by zero or infinity, or multiplying infinities, etc.). – lulutanseco Sep 29 '16 at 03:52
  • @lulutanseco - What's the type of the value stored in `Session("result")`? – Enigmativity Sep 29 '16 at 04:11
  • It was supposed to be a float number resulting fro the average of prices for the day(s) selected by the client. However, if the client selected nothing, the session("result") will be an indefinite number because we are dividing a number by zero. – lulutanseco Sep 29 '16 at 04:15
  • Double does not have indefinite or undefined. It has `Double.NaN`, `Double.PositiveInfinity` and `Double.NegativeInfinity` – Slai Sep 29 '16 at 04:30

1 Answers1

2

System.Decimal does not have NaN or infinity, so you can use it instead:

Dim valid = Decimal.TryParse(Session("result").ToString, x)

It will result in False if Session("result") is Double.NaN, .PositiveInfinity or .NegativeInfinity

Slai
  • 22,144
  • 5
  • 45
  • 53