Note that there are some explanatory texts on larger screens.

plurals
  1. POConversion from string "." to type 'Decimal' is not valid
    text
    copied!<p>I have a unit converter written in Visual Basic, using Visual Studio 2013. The program is working fine until the user's initial input is a decimal point. I get this error message: Conversion from string "." to type 'Decimal' is not valid. How can I get this program to accept a decimal point as the initial input from the user without the program crashing? Code is below.</p> <pre><code>Private Function GetLength1(ByVal decLengthUnit1 As Decimal) As Decimal Dim decResult1 As Decimal If cboUnitType.SelectedItem = "Length" Then ' converts kilometer to... If cbo1.SelectedItem = "Kilometer" Then If cbo2.SelectedItem = "Kilometer" Then decResult1 = txtUnit1.Text ElseIf cbo2.SelectedItem = "Meter" Then decResult1 = (decLengthUnit1 * 1000) ElseIf cbo2.SelectedItem = "Centimeter" Then decResult1 = (decLengthUnit1 * 100000) ElseIf cbo2.SelectedItem = "Millimeter" Then decResult1 = (decLengthUnit1 * 1000000) ElseIf cbo2.SelectedItem = "Mile" Then decResult1 = (decLengthUnit1 * 0.621371191) ElseIf cbo2.SelectedItem = "Yard" Then decResult1 = (decLengthUnit1 * 1093.613297) ElseIf cbo2.SelectedItem = "Foot" Then decResult1 = (decLengthUnit1 * 3280.83989) ElseIf cbo2.SelectedItem = "Inch" Then decResult1 = (decLengthUnit1 * 39370.07868) End If End If End If Return decResult1.ToString().Trim("0") ' this is where I convert the data back to a string with some formatting End Function Private Sub txtUnit1_TextChanged(sender As Object, e As EventArgs) Handles txtUnit1.TextChanged ' convert string to numeric data type Decimal.TryParse(txtUnit1.Text, decUnit1) ' this is where I convert user input to decimal data type ' handle String.Empty, or negative sign If txtUnit1.Text = "" OrElse txtUnit1.Text = "-" Then txtUnit2.Text = "" ElseIf cboUnitType.SelectedItem = "Length" Then suppressTextBox2TextChanged = True txtUnit2.Text = GetLength1(decUnit1) suppressTextBox2TextChanged = False End If End Sub </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload