none
CSng/CDbl both return 0.0 when converting a 0 value. Is this a behavior change from VB6?

    Question

  • I am converting a VB6 app to VB.net and I have noticed that when a 0 value is passed to both a CSng or CDbl functio:

    VB6 returns = 0

    VB.Net returns = 0.0

    Is this a behavior change in VB.net?

    Thursday, December 08, 2016 4:59 PM

Answers

  • I am converting a VB6 app to VB.net and I have noticed that when a 0 value is passed to both a CSng or CDbl functio:

    VB6 returns = 0

    VB.Net returns = 0.0

    Is this a behavior change in VB.net?

    Hi,

    this is IMHO correct behaviour, since 0 is a literal for an Integer, 0.0 for a double and 0.0F for a float (Single).

    Edit: Could you post an example please, Here I get 0 from cdbl...

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim a As Double = 0
            'note that this will return "0"
            MsgBox(a.ToString)
            Dim b As Integer = 0
            MessageBox.Show(CDbl(b).ToString)
            Dim c As Double = CDbl(b)
            Console.WriteLine(c) 'writes "0"
        End Sub

    Regards,

      Thorsten



    Thursday, December 08, 2016 5:24 PM

All replies

  • I am converting a VB6 app to VB.net and I have noticed that when a 0 value is passed to both a CSng or CDbl functio:

    VB6 returns = 0

    VB.Net returns = 0.0

    Is this a behavior change in VB.net?

    Hi,

    this is IMHO correct behaviour, since 0 is a literal for an Integer, 0.0 for a double and 0.0F for a float (Single).

    Edit: Could you post an example please, Here I get 0 from cdbl...

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim a As Double = 0
            'note that this will return "0"
            MsgBox(a.ToString)
            Dim b As Integer = 0
            MessageBox.Show(CDbl(b).ToString)
            Dim c As Double = CDbl(b)
            Console.WriteLine(c) 'writes "0"
        End Sub

    Regards,

      Thorsten



    Thursday, December 08, 2016 5:24 PM
  • Thank you.  I will need to modify many IF/Then that reference

    If Csng(myValue) <> 0 then.......Do something.

    In VB6 that was valid.  In .Net apparently that is no longer the case (as you noted above).

    Thank you.

    Thursday, December 08, 2016 5:28 PM
  • Hi,

    see the Edit section of my previous post. I get 0 from the conversion...

    Regards,

      Thorsten

    Thursday, December 08, 2016 5:34 PM
  • Thank you.  I will need to modify many IF/Then that reference

    If Csng(myValue) <> 0 then.......Do something.

    In VB6 that was valid.  In .Net apparently that is no longer the case (as you noted above).

    Thank you.

    This is still valid:

    Option Strict On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim a As Double = 0
            'note that this will return "0"
            MsgBox(a.ToString)
            Dim b As Integer = 0
            MessageBox.Show(CDbl(b).ToString)
            Dim c As Double = CDbl(b)
            Console.WriteLine(c) 'writes "0"
            c += 0.1
    
            If CSng(c) <> 0 Then
                MessageBox.Show(c.ToString())
            End If
    
            b += 1
            If CSng(b) <> 0 Then
                MessageBox.Show(b.ToString())
            End If
        End Sub
    End Class

    This code compiles and shows all messageboxes...

    Regards,

      Thorsten

    Thursday, December 08, 2016 5:36 PM
  •  Here is my Function....
    For the sake of discussion strValue="0" 

    Public Function Field2NumZero(ByRef vntField As Object) As Single
            Dim strValue As String
            strValue = ObjectValue2String(vntField)

            If IsDBNull(strValue) Then
                Field2NumZero = 0
            ElseIf Not IsNumeric(strValue) Then
                Field2NumZero = 0
            Else
                Field2NumZero = CSng(strValue)
            End If
        End Function

    Output:
    Field2NumZero = CSng(strValue) RESULT: 0.0

    Based on your eample:
    Field2NumZero = CSng(strValue).ToString RESULT: "0" (but that wouldnt work for me because I need to Return a Single

    Thursday, December 08, 2016 5:41 PM
  • when hovering the mouse over the 0, I get an int structure for that 0, but hovering over <> gives the comparison for Single, Single, so, the conversion of the 0 to a Single is done implicitely:

    Regards,

      Thorsten

    Thursday, December 08, 2016 5:42 PM
  •  Here is my Function....
    For the sake of discussion strValue="0" 

    Public Function Field2NumZero(ByRef vntField As Object) As Single
            Dim strValue As String
            strValue = ObjectValue2String(vntField)

            If IsDBNull(strValue) Then
                Field2NumZero = 0
            ElseIf Not IsNumeric(strValue) Then
                Field2NumZero = 0
            Else
                Field2NumZero = CSng(strValue)
            End If
        End Function

    Output:
    Field2NumZero = CSng(strValue) RESULT: 0.0

    Based on your eample:
    Field2NumZero = CSng(strValue).ToString RESULT: "0" (but that wouldnt work for me because I need to Return a Single

    I suggest to use one of the TryParse functions for string to numeric conversion like:

            Dim strValue As String
            strValue = "0"
    
            Dim Field2NumZero As Single = -10.0F 'initialize with something else than 0, since we want to get the output 0...
    
            If IsDBNull(strValue) Then
                Field2NumZero = 0
            ElseIf Not IsNumeric(strValue) Then
                Field2NumZero = 0
            Else
                If Single.TryParse(strValue, Field2NumZero) Then
                    'so something
                    MsgBox(Field2NumZero.ToString())
                Else
                    MessageBox.Show("input string has a wrong format...")
                End If
            End If

    Regards,

      Thorsten


    Thursday, December 08, 2016 5:47 PM
  • ... see:

    https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx

    and

    https://msdn.microsoft.com/en-us/library/0xh24xh4(v=vs.110).aspx

    Regards,

      Thorsten

    Thursday, December 08, 2016 5:48 PM
  • ... see:

    https://msdn.microsoft.com/en-us/library/26sxas5t(v=vs.110).aspx

    and

    https://msdn.microsoft.com/en-us/library/0xh24xh4(v=vs.110).aspx

    Regards,

      Thorsten

    Thank you Thorsten. I am going to review what you suggested and see what modifications I need to make.
    Thursday, December 08, 2016 5:50 PM
  • Hi MrAvgProgrammer,

    Welcome to the MSDN forum.

    Does your issue is solved or not? If you think Thorsten's reply is helpful for you, you can try to mark it as answer, that will help other community members who have the same or similar issue to easier search this useful solution, thank you for your support.

    Best regards,

    Sara


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 09, 2016 2:57 AM
    Moderator