How to get rid of NaN

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I have a bunch of formulas in my code. Some are in DGV Cells and some in texboxes. In some before the all the data is processed it shows NaN and I'd prefer it didn't do that.

This formula doesn't do show NaN in the textbox

VB.NET:
Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
        ' For Changes to Valve Area


        Dim count = Val(Me.TextBox7.Text) ' Number of valves
        Dim vdiam = Val(Me.TextBox1.Text) ' Valve Diameter
        Dim varea = vdiam * vdiam * 0.7854 'Valve Area (Diameter^2 * Pi / 4)


        For Each r As DataGridViewRow In Me.DGV1.Rows


            'Populates CFM -Sq.In Column using formula = CFM / Valve Area 
            r.Cells(4).Value = CDbl(r.Cells(2).Value) / varea


            'Valve Area calculation
            Me.TextBox9.Text = CStr(Val(varea * count))
        Next
    End Sub

While this one does NOT show NaN

VB.NET:
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
        'Average Port Area Textbox - Populates Average Velocity  [Column 9 of DGV] With Formulated Values
        Dim Roof = Val(TextBox4.Text)
        Dim Floor = Val(TextBox5.Text)
        Dim CLine = Val(TextBox4.Text) + Val(TextBox5.Text) / 2
        Dim Pvol = Val(TextBox6.Text)


        For Each r As DataGridViewRow In Me.DGV1.Rows
            'Average Port Velocity Formula = ( Flow_CFM * 2.4 ) / Average_CSA 
            r.Cells(9).Value = (CDbl(r.Cells(2).Value) * 2.4) / Val(Me.TextBox8.Text)


            'Average Port Area = Port_Volume_CC / (Port_CenterLine_Length * 16.387)
            Me.TextBox8.Text = CStr(Pvol / (CLine * 16.387))


        Next
    End Sub
 
NaN is a Double value that represents a result that is Not a Number. If you don't want to display the value NaN in the grid then don;t display that value in the grid. You're displaying the result without actually checking whether it's NaN or not. It's not going to magically read your mind and not display a particular value. It's up to you to check whether the result is NaN and display something else or nothing in that case.
 
If Double.IsNaN(myDouble) Then
is the sort of thing you need to do.
 

Latest posts

Back
Top