Question String to double

Fury

Active member
Joined
Oct 12, 2007
Messages
28
Programming Experience
Beginner
I have deployed an application for stock administration.
The user can make products with price and length...
Both have to be decimal so I converted the strings to doubles.
Everything works on the development computer also on another computer and a portable. The problem is only on the client's computer where there seems to be a problem with the conversion. Allways getting "Can not convert to double" error. How can that be when it's working on 3 other computers??
 
Hello.

The question would be: What is he trying to convert or what is the client trying to enter?
Are you using a textbox for the input? If so you should switch to a NumericUpDown which is directly delivering a Double (or was it decimal?) as Value. Also you should really check what the client is entering (using if isnumeric()). Within a string a fullstop and a comma can make a huge difference depending on the local settings of the computer.

Bobby
 
Thank you for your reply.
They can only enter values like 7,5.
I programmed it so that a full stop is allways entered as a comma.

So it can be that they need a full stop depending on there local settings?
That was something I was thinking about.

How can I program to look for local settings so that the application knows if a full stop or a comma is needed?
 
Yes, this can be. F.e. Within Europe the numbers are like 1.255.725,33 , in America and other countries it's turned around like 1,255,725.33 . You can check this by using the My.Computer Namespace:
VB.NET:
My.Computer.Info.InstalledUICulture.NumberFormat

Is the spacebar also blocked? If not, that's a potential error source. .NET can convert numbers like " 5,5 ", but not "5, 5".

Bobby
 
Yes, the spacebar is also blocked.
I will use this code:
VB.NET:
My.Computer.Info.InstalledUICulture.NumberFormat.NumberDecimalSeparator
Thanks ;), I think that will be the problem.
 
So there regional settings are correct. :confused:
Still they have this error that the string couldn't be converted.
Now I really don't know what the problem could be.
 
The first part is validation code.
VB.NET:
If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf e.KeyChar = vbBack Then
            e.Handled = False
        ElseIf e.KeyChar = "." Then
            If TextBoxLength.Text.IndexOf(",") > -1 Then
                e.Handled = True
            Else
                e.KeyChar = CChar(",")
            End If
        ElseIf e.KeyChar = "," Then
            If TextBoxLength.Text.IndexOf(",") > -1 Then
                e.Handled = True
            Else
                e.Handled = False
            End If
            Else
                e.Handled = True
        End If

        If TextBoxLength.Text.Length = 0 Then
            If e.KeyChar = "," Or e.KeyChar = "." Then
                e.Handled = True
            End If
        End If
The second part is where I cast string length into double.
VB.NET:
Dim profile As New BusinessEntity.Profile

                    With profile

                        .ProductName = TextBoxDescription.Text
                        .Min = CType(TextBoxMin.Text, Integer)
                        .Max = CType(TextBoxMax.Text, Integer)
                        .VitraluxCode = TextBoxVitraluxcode.Text
                        .Length = CType(TextBoxLength.Text, Double)
                        If CheckBoxThermal.Checked Then
                            .Thermal = True
                        Else
                            .Thermal = False
                        End If
                        .Activity = True
                        .Type = "Profiel"
                        If ComboBoxColor.Enabled Then
                            .ColorID = CType(ComboBoxColor.SelectedValue, Integer)
                        Else
                            .ColorID = Nothing
                        End If

                    End With
I hope you can help me with this.
 
That's pretty odd...but hard to say without access to a computer where it's not working.

What's the exact error Message? (A little guessing is within this, since I'm using a German Visual Studio)
VB.NET:
String  couldn't be converted to Double
or
VB.NET:
String 7,5 couldn't be converted to Double

Normally he tries to print the string, which he couldn't convert, within the error message (as shown above). If there's no string but a double space then there's something really wrong, like he couldn't read the Text property.

Normally it could only be a faulty character, but I can't imagine that your client is entering Alt-Codes or something similar. You could also try to use CDbl instead, since this is an explicit conversion to Double, but I don't think that this would make any difference...but maybe.

Sorry, but this is a really weird problem...I'm just guessing at the moment...
 
Back
Top