Home > database >  How To Check If A Variable From Double Type Doesn't Have A Value. VB.NET
How To Check If A Variable From Double Type Doesn't Have A Value. VB.NET

Time:02-06

my problem in VB.NET.

I hope you see my code.not all the my code is important.

the important part is in the second if condition and which is if snum = "".

the snum variable is a variable from double type and i check if this variable is empty. i mean it doesn't have any value.

and when i run the code , the program show me this error on the second if condition which i told you about.

""Additional information: Conversion from string "" to type 'Double' is not valid.""

I work on a program to move motors. and the code which i wrote is one of the things that i do in the program.

why i check if snum = "" ??

because the value of snum is the coordinates of one of the motors and it is Z Axis Motor. and the coordinates are written in TextBox.

i took the coordinates of Z Axis Motor from the TextBox and put it in snum variable. then i check if the snum variable is empty by this code if snum == "". if snum is empty then the Z Axis Motor will not move. and if snum is not empty and even if its value is 0 the Z Axis Motor Will Move.

so how can i check if a variable from double type doesn't have a value ?

enve if its value is 0 the motor will move. i want to check if it doesn't have any value.

this is the code. the procedure works on button click

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim snum As Double
        Dim tnum As Double
        Dim h As Integer = 0
        If h = 1 Then
            snum = TextBox1.Text
            tnum = TextBox1.Text
            TextBox3.Text = snum
            TextBox4.Text = tnum
        Else
            If snum = "" Then
                snum = tnum
                TextBox2.Text = "Hello , How Are You"
                TextBox3.Text = snum
                TextBox4.Text = tnum
            End If
        End If

    End Sub

CodePudding user response:

Jmcilhinney is right: this code does not have much use. However here is how to convert the text variables to double and vice versa with CDBL () and Tostring:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim snum As Double
    Dim tnum As Double
    Dim h As Integer = 0
    If h = 1 Then
        snum = cDbl(TextBox1.Text)
        tnum = cDbl(TextBox1.Text)
        TextBox3.Text = snum.toString
        TextBox4.Text = tnum.toString
    Else
        If snum = 0 Then
            snum = tnum
            TextBox2.Text = "Hello , How Are You"
            TextBox3.Text = snum.toString
            TextBox4.Text = tnum.toString
        End If
    End If

End Sub

CodePudding user response:

Your code still makes little sense as it is but I'll answer in a more general sense about value types. Double is a value type and therefore ALWAYS has a value. When you declare a variable, under the hood, the memory allocated to that variable will contain all zeroes by default and that is represented by Nothing in VB. What that actually means depends on the type. For all reference types (i.e. classes) it represents no object. For all value types (i.e. structures) it represents the default value of that type, e.g. False for Boolean, #1/01/0001# for Date and zero for all numeric types. That means that, in your case, your Double variables will have the value 0.0.

If you want to be able to represent no value for a value type then you have to use nullable value types, e.g. Nullable(Of Double) or Double? for short. You can test the HasValue property of a nullable value type to see whether it has a value and, if it does, get that value from the Value property. For a Nullable(Of T)/T?, the Value property will be type T. Note that getting the Value property when HasValue is False will throw an exception.

Dim d1 As Double?
Dim d2 As Double?

If d1.HasValue Then
    Console.WriteLine("d1 = " & d1.Value)
Else
    Console.WriteLine("d1 has no value")
End If

If d2.HasValue Then
    Console.WriteLine("d2 = " & d2.Value)
Else
    Console.WriteLine("d2 has no value")
End If

d1 = 0.0
d2 = 123.456

If d1.HasValue Then
    Console.WriteLine("d1 = " & d1.Value)
Else
    Console.WriteLine("d1 has no value")
End If

If d2.HasValue Then
    Console.WriteLine("d2 = " & d2.Value)
Else
    Console.WriteLine("d2 has no value")
End If
  • Related