VS 2010 How to Replace NaN with zero or a message?-VBForums
Results 1 to 3 of 3

Thread: How to Replace NaN with zero or a message?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Location
    PH
    Posts
    10

    Smile How to Replace NaN with zero or a message?

    I tried anything like in if statement but I dont know what should i type in order to replace NaN with a zero or a message like "empty"


    please help me guys im just new to Ms Visual Studio 2010

    thanks in advance





    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
    If txtA.Text = Nothing Then
    MessageBox.Show("textbox A value is Missing! please input a value", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtA.Focus()
    Exit Sub
    End If
    If txtB.Text = Nothing Then
    MessageBox.Show("textbox B value is Missing! please input a value", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtB.Focus()
    Exit Sub
    End If
    If txtC.Text = Nothing Then
    MessageBox.Show("textbox C value is Missing! please input a value", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    txtC.Focus()
    Exit Sub
    End If


    Dim a, b, c, x1, x2 As Double


    a = Val(txtA.Text)
    b = Val(txtB.Text)
    c = Val(txtC.Text)

    x1 = Format(Val(-b + Math.Sqrt(b ^ 2 - 4 * a * c)) / (2 * a), "#,###.00")

    x2 = Format(Val(-b - Math.Sqrt(b ^ 2 - 4 * a * c)) / (2 * a), "#,###.00")

    lblOutput2.Text = "{" & x1 & " , " & x2 & "}"

    If txtA.Text = Nothing Then
    lblOutput2.Text = "no value computed!"
    ElseIf txtB.Text = Nothing Then
    lblOutput2.Text = "no Value Computed!"
    ElseIf txtC.Text = Nothing Then
    lblOutput2.Text = "no value Computed!"
    ElseIf txtA.Text = 0 Then
    MessageBox.Show("Invalid Divisor!, put a valid numeric VALUE!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    lblOutput2.Text = "Invalid"
    ElseIf lblOutput2.Text = is Then
    lblOutput2.Text = "ERROR!"
    End If

    End Sub

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,267

    Re: How to Replace NaN with zero or a message?

    Actually, rather than replacing NAN, you should perform a check before the function to ensure that the result is never NAN. This will require a couple steps.

    For one thing, Val is risky. It would be better to use Double.TryParse, but that won't truly solve the problem. After all, no matter what you do, if a=0, the equations will result in NAN. The risk of Val is that if the user enters a comma, you will get a number, but not the number you expected, and so forth. However, if you check that a <> 0, then you can avoid performing the equation for cases when a = 0, and NAN will never occur. You can scold the user if you wish, at that point.

    By the way, txtA.Text will never be Nothing, and that isn't the way to test for Nothing anyways. It would look like:

    If txtA.Text Is Nothing Then

    However, since it will never be nothing, that isn't really worth doing. Check for this:

    If txtA.Text = String.Empty Then

    or else

    If txtA.Text = "" Then
    My usual boring signature: Nothing

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    55

    Re: How to Replace NaN with zero or a message?

    Additionally, in VS 2010, there is a new string function String.IsNullOrWhiteSpace(txtA.text) which checks if the textbox is empty, equals Nothing (which in the case of the textbox is never the case), or is just filled with white space.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.