Next Generation Emulation banner
1 - 4 of 4 Posts

·
Registered
Joined
·
246 Posts
Discussion Starter · #1 ·
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim d1 As Double
        d1 = CDbl("12.2")
        TextBox1.Text = CStr(d1)
    End Sub
End Class
can anyone tell me why this won't work
i get this error:
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

if however i write "12,2" (using comma instead, it works)
but withouth conversion it only accepts periods.

is it supposed to work like that?
 

·
Premium Member
Joined
·
17,148 Posts
mmm that´s really interesting and it works fine here...anyways i´ve seen similar problems while using a German OS and working with Decimal values on SQL databases in a English SQL Server.. atm am using a english one tho.

anyways this code works great for me:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim d1 As Double
        d1 = CDbl("12,2")
        TextBox1.Text = d1.ToString()
    End Sub
End Class
btw i used the method .ToString() instead of CStr() because i think its better..

anyways you could use a Try-Catch and post the message here so i can see more info about the problem.. something like that:
Code:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim d1 As Double
        Try
            d1 = CDbl("12,2")
            TextBox1.Text = d1.ToString()
        Catch ex As Exception
            Throw New ArgumentException(ex.Message)
        End Try
    End Sub
End Class
other than that i recommend you to set a break point at the start of the page_load event and step through(F10 or F11) and find out where it fails and in that way see the reason why it fail to cast??? at least it seems to be the problem.
 

·
Registered
Joined
·
246 Posts
Discussion Starter · #3 ·
it seems like if the decimal symbol in the regional settings is set to comma(,) it won't accept periods(.) as a decimal sign when converting from string to double, though in the code it still uses periods.

so when i changed the decimal symbol from , to . in the regional settings it worked just fine.
 
1 - 4 of 4 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top