Prevent NaN result in VB

SeanPress 166 Reputation points
2021-02-03T22:41:59.887+00:00

I have the below calculation in my windows form, how do I make the calculation return a zero in the Label rather than NaN if any of the TextBox text is zero:

If txtLA1 = False And txtPA1 = False And txtOA1 = False Then
lblSA.Text = Math.Round(Val(txtLA.Text * txtOA.Text) / Val(txtPA.Text * 735000), 1)
End If

I’m new to VB and I’v been searching on the net for a few days. I know NaN is being returned because the calculation is dividing by zero if the TextBox has a zero but I can’t find anything that meets my needs.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,511 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,539 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
924 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
323 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,011 Reputation points
    2021-02-04T01:44:39.203+00:00

    Hello,

    • Never use Val, that is not the .NET way but the VB6 way and best to stay away from VB6 coding
    • Always assume a value is not a number (NaN)
    • If this was JavaScript we would use isNaN e.g. if (Number.isNaN(someObject.someValue)) ...

    Simple example (the Integer.TryParse could also be Double.TryParse, Decimal.TryParse). You should add an "Else" to the "If" statement to inform the user one of the numbers is not a number.

    Public Class Form1
        Private Sub CalulateButton_Click(sender As Object, e As EventArgs) Handles CalulateButton.Click
            Dim value1 As Integer
            Dim value2 As Integer
    
            If Integer.TryParse(TextBox1.Text, value1) AndAlso Integer.TryParse(TextBox2.Text, value2) Then
                Label1.Text = $"{Math.Round(value1) / value2}"
            End If
        End Sub
    End Class
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Adrian Bowles 86 Reputation points
    2021-02-14T21:58:08.347+00:00

    VB has a simple IsNumeric function that is contained in every version

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.information.isnumeric?view=net-5.0

    C# doesn't have it but you can create a simple extension method to implement it - which uses the TryParse method. But that seems to be something that is asked and not present whereas VB has it as part of the runtime.

    0 comments No comments