VB.net: “Input string was not in a correct format.” Using Convert.ToDouble












-1















I keep receiving the error mention in the title. I've tried replacing all "+" with "&" as mentioned in other threads, but it doesn't seem to work. I wanted to create a program that allows me to see my weight on other planets using VB.net but I cannot get past this "Input string was not in a correct format" error. Any help please? The error occurs at the first line.



                    Convert.ToDouble(ComboBox1.SelectedItem())
If ComboBox1.SelectedItem = 0 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.378)
ElseIf ComboBox1.SelectedItem = 1 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.905)
ElseIf ComboBox1.SelectedItem = 2 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.0)
ElseIf ComboBox1.SelectedItem = 3 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.379)
ElseIf ComboBox1.SelectedItem = 4 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 2.529)
ElseIf ComboBox1.SelectedItem = 5 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.066)
ElseIf ComboBox1.SelectedItem = 6 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.903)
ElseIf ComboBox1.SelectedItem = 7 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.096)
ElseIf ComboBox1.SelectedItem = 8 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.069)
End If









share|improve this question


















  • 2





    Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

    – LarsTech
    Nov 20 '18 at 20:44













  • It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

    – soohoonigan
    Nov 20 '18 at 20:49











  • Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

    – Mary
    Nov 20 '18 at 20:51











  • Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

    – soohoonigan
    Nov 20 '18 at 20:51











  • Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

    – Ahmed Abdelhameed
    Nov 20 '18 at 20:51


















-1















I keep receiving the error mention in the title. I've tried replacing all "+" with "&" as mentioned in other threads, but it doesn't seem to work. I wanted to create a program that allows me to see my weight on other planets using VB.net but I cannot get past this "Input string was not in a correct format" error. Any help please? The error occurs at the first line.



                    Convert.ToDouble(ComboBox1.SelectedItem())
If ComboBox1.SelectedItem = 0 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.378)
ElseIf ComboBox1.SelectedItem = 1 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.905)
ElseIf ComboBox1.SelectedItem = 2 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.0)
ElseIf ComboBox1.SelectedItem = 3 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.379)
ElseIf ComboBox1.SelectedItem = 4 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 2.529)
ElseIf ComboBox1.SelectedItem = 5 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.066)
ElseIf ComboBox1.SelectedItem = 6 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.903)
ElseIf ComboBox1.SelectedItem = 7 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.096)
ElseIf ComboBox1.SelectedItem = 8 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.069)
End If









share|improve this question


















  • 2





    Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

    – LarsTech
    Nov 20 '18 at 20:44













  • It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

    – soohoonigan
    Nov 20 '18 at 20:49











  • Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

    – Mary
    Nov 20 '18 at 20:51











  • Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

    – soohoonigan
    Nov 20 '18 at 20:51











  • Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

    – Ahmed Abdelhameed
    Nov 20 '18 at 20:51
















-1












-1








-1








I keep receiving the error mention in the title. I've tried replacing all "+" with "&" as mentioned in other threads, but it doesn't seem to work. I wanted to create a program that allows me to see my weight on other planets using VB.net but I cannot get past this "Input string was not in a correct format" error. Any help please? The error occurs at the first line.



                    Convert.ToDouble(ComboBox1.SelectedItem())
If ComboBox1.SelectedItem = 0 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.378)
ElseIf ComboBox1.SelectedItem = 1 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.905)
ElseIf ComboBox1.SelectedItem = 2 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.0)
ElseIf ComboBox1.SelectedItem = 3 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.379)
ElseIf ComboBox1.SelectedItem = 4 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 2.529)
ElseIf ComboBox1.SelectedItem = 5 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.066)
ElseIf ComboBox1.SelectedItem = 6 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.903)
ElseIf ComboBox1.SelectedItem = 7 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.096)
ElseIf ComboBox1.SelectedItem = 8 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.069)
End If









share|improve this question














I keep receiving the error mention in the title. I've tried replacing all "+" with "&" as mentioned in other threads, but it doesn't seem to work. I wanted to create a program that allows me to see my weight on other planets using VB.net but I cannot get past this "Input string was not in a correct format" error. Any help please? The error occurs at the first line.



                    Convert.ToDouble(ComboBox1.SelectedItem())
If ComboBox1.SelectedItem = 0 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.378)
ElseIf ComboBox1.SelectedItem = 1 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.905)
ElseIf ComboBox1.SelectedItem = 2 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.0)
ElseIf ComboBox1.SelectedItem = 3 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.379)
ElseIf ComboBox1.SelectedItem = 4 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 2.529)
ElseIf ComboBox1.SelectedItem = 5 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.066)
ElseIf ComboBox1.SelectedItem = 6 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.903)
ElseIf ComboBox1.SelectedItem = 7 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 1.096)
ElseIf ComboBox1.SelectedItem = 8 Then
Label4.Text = "Your weight on " + ComboBox1.SelectedItem() + " is " + (TextBox1.Text * 0.069)
End If






vb.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 20:39









surrealismsurrealism

1




1








  • 2





    Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

    – LarsTech
    Nov 20 '18 at 20:44













  • It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

    – soohoonigan
    Nov 20 '18 at 20:49











  • Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

    – Mary
    Nov 20 '18 at 20:51











  • Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

    – soohoonigan
    Nov 20 '18 at 20:51











  • Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

    – Ahmed Abdelhameed
    Nov 20 '18 at 20:51
















  • 2





    Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

    – LarsTech
    Nov 20 '18 at 20:44













  • It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

    – soohoonigan
    Nov 20 '18 at 20:49











  • Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

    – Mary
    Nov 20 '18 at 20:51











  • Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

    – soohoonigan
    Nov 20 '18 at 20:51











  • Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

    – Ahmed Abdelhameed
    Nov 20 '18 at 20:51










2




2





Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

– LarsTech
Nov 20 '18 at 20:44







Always use TryParse instead. You are assuming SelectedItem is a number. Enable "Option Strict On" at the top of your file.

– LarsTech
Nov 20 '18 at 20:44















It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

– soohoonigan
Nov 20 '18 at 20:49





It seems, from your code, that Combobox1.SelectedItem() is probably holding the name of a planet, so converting "Mars" to a double clearly wouldn't work. Perhaps you should be converting TextBox1.Text to double, which looks like it is supposed to be holding a number?

– soohoonigan
Nov 20 '18 at 20:49













Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

– Mary
Nov 20 '18 at 20:51





Do you mean .SelectedIndex? The .Text property of a TextBox is a string. Don't try to multiply strings by numbers even if it appears to work sometimes. Turning on Option Strict will save you odd runtime errors. A user is not guaranteed to enter what you expect.

– Mary
Nov 20 '18 at 20:51













Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

– soohoonigan
Nov 20 '18 at 20:51





Also, in your "If" statements, I think the Combobox1.SelectedIndex is the property you are after, which will hold an integer. SelectedItem, when cast to a string, will have the planet's name

– soohoonigan
Nov 20 '18 at 20:51













Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

– Ahmed Abdelhameed
Nov 20 '18 at 20:51







Looks like you intended to check the value ofSelectedIndex, not the SelectedItem?

– Ahmed Abdelhameed
Nov 20 '18 at 20:51














2 Answers
2






active

oldest

votes


















0














Thank you all for your help! I solved the problem, by adding CDbl() just before the user input, used Option Strict On, and used SelectedIndex() instead of SelectedItem().



Here is the code:



If ComboBox1.SelectedIndex = 0 Then
Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
ElseIf ComboBox1.SelectedIndex = 1 Then
Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
ElseIf ComboBox1.SelectedIndex = 2 Then
Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
ElseIf ComboBox1.SelectedIndex = 3 Then
Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
ElseIf ComboBox1.SelectedIndex = 4 Then
Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
ElseIf ComboBox1.SelectedIndex = 5 Then
Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
ElseIf ComboBox1.SelectedIndex = 6 Then
Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
ElseIf ComboBox1.SelectedIndex = 7 Then
Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
ElseIf ComboBox1.SelectedIndex = 8 Then
Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
End If





share|improve this answer



















  • 1





    Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

    – Ahmed Abdelhameed
    Nov 20 '18 at 22:03



















0














Use data-binding instead of If else

and Double.TryParse instead of CDbl for conversions from string to integers

and & instead of + when combining strings(or String interpolation).




Option Strict On - always (which you already did)




Public Class Planet
Public ReadOnly Property Name As String
Public ReadOnly Property WeightFactor As Double

Public Sub New(name As String, weightFactor As Double)
Me.Name = name
Me.WeightFactor = weightFactor
End Sub
End Class

' Create planets and bind it to the ComboBox
' Form constructor
Public Sub New()
InitializeComponent()

Dim planets As New List(Of Planet) From
{
New Planet("Mercury", 0.378),
New Planet("Venus", 0.905),
New Planet("Earth", 1.0),
New Planet("Mars", 0.379),
}

' Bind planets to combobox
ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text
ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value
ComboBox1.DataSource = planets
End Sub


Then your calculation code will look simpler



Dim personWeight AS Double
If Double.TryParse(textbox1.Text, personWeight) Then
' SelectedItem is instance of selected planet
Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

Dim weightOnPlanet = personWeight * planet.WeightFactor
Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
End If





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401161%2fvb-net-input-string-was-not-in-a-correct-format-using-convert-todouble%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thank you all for your help! I solved the problem, by adding CDbl() just before the user input, used Option Strict On, and used SelectedIndex() instead of SelectedItem().



    Here is the code:



    If ComboBox1.SelectedIndex = 0 Then
    Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
    ElseIf ComboBox1.SelectedIndex = 1 Then
    Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
    ElseIf ComboBox1.SelectedIndex = 2 Then
    Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
    ElseIf ComboBox1.SelectedIndex = 3 Then
    Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
    ElseIf ComboBox1.SelectedIndex = 4 Then
    Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
    ElseIf ComboBox1.SelectedIndex = 5 Then
    Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
    ElseIf ComboBox1.SelectedIndex = 6 Then
    Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
    ElseIf ComboBox1.SelectedIndex = 7 Then
    Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
    ElseIf ComboBox1.SelectedIndex = 8 Then
    Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
    End If





    share|improve this answer



















    • 1





      Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

      – Ahmed Abdelhameed
      Nov 20 '18 at 22:03
















    0














    Thank you all for your help! I solved the problem, by adding CDbl() just before the user input, used Option Strict On, and used SelectedIndex() instead of SelectedItem().



    Here is the code:



    If ComboBox1.SelectedIndex = 0 Then
    Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
    ElseIf ComboBox1.SelectedIndex = 1 Then
    Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
    ElseIf ComboBox1.SelectedIndex = 2 Then
    Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
    ElseIf ComboBox1.SelectedIndex = 3 Then
    Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
    ElseIf ComboBox1.SelectedIndex = 4 Then
    Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
    ElseIf ComboBox1.SelectedIndex = 5 Then
    Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
    ElseIf ComboBox1.SelectedIndex = 6 Then
    Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
    ElseIf ComboBox1.SelectedIndex = 7 Then
    Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
    ElseIf ComboBox1.SelectedIndex = 8 Then
    Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
    End If





    share|improve this answer



















    • 1





      Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

      – Ahmed Abdelhameed
      Nov 20 '18 at 22:03














    0












    0








    0







    Thank you all for your help! I solved the problem, by adding CDbl() just before the user input, used Option Strict On, and used SelectedIndex() instead of SelectedItem().



    Here is the code:



    If ComboBox1.SelectedIndex = 0 Then
    Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
    ElseIf ComboBox1.SelectedIndex = 1 Then
    Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
    ElseIf ComboBox1.SelectedIndex = 2 Then
    Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
    ElseIf ComboBox1.SelectedIndex = 3 Then
    Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
    ElseIf ComboBox1.SelectedIndex = 4 Then
    Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
    ElseIf ComboBox1.SelectedIndex = 5 Then
    Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
    ElseIf ComboBox1.SelectedIndex = 6 Then
    Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
    ElseIf ComboBox1.SelectedIndex = 7 Then
    Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
    ElseIf ComboBox1.SelectedIndex = 8 Then
    Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
    End If





    share|improve this answer













    Thank you all for your help! I solved the problem, by adding CDbl() just before the user input, used Option Strict On, and used SelectedIndex() instead of SelectedItem().



    Here is the code:



    If ComboBox1.SelectedIndex = 0 Then
    Label4.Text = "Your weight on Mercury" & " is " & (CDbl(TextBox1.Text) * 0.378)
    ElseIf ComboBox1.SelectedIndex = 1 Then
    Label4.Text = "Your weight on Venus" & " is " & (CDbl(TextBox1.Text) * 0.905)
    ElseIf ComboBox1.SelectedIndex = 2 Then
    Label4.Text = "Your weight on Earth" & " is " & (CDbl(TextBox1.Text) * 1.0)
    ElseIf ComboBox1.SelectedIndex = 3 Then
    Label4.Text = "Your weight on Mars" & " is " & (CDbl(TextBox1.Text) * 0.379)
    ElseIf ComboBox1.SelectedIndex = 4 Then
    Label4.Text = "Your weight on Jupiter" & " is " & (CDbl(TextBox1.Text) * 2.529)
    ElseIf ComboBox1.SelectedIndex = 5 Then
    Label4.Text = "Your weight on Saturn" & " is " & (CDbl(TextBox1.Text) * 1.066)
    ElseIf ComboBox1.SelectedIndex = 6 Then
    Label4.Text = "Your weight on Uranus" & " is " & (CDbl(TextBox1.Text) * 0.903)
    ElseIf ComboBox1.SelectedIndex = 7 Then
    Label4.Text = "Your weight on Neptune" & " is " & (CDbl(TextBox1.Text) * 1.096)
    ElseIf ComboBox1.SelectedIndex = 8 Then
    Label4.Text = "Your weight on Pluto" & " is " & (CDbl(TextBox1.Text) * 0.069)
    End If






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 21:13









    surrealismsurrealism

    1




    1








    • 1





      Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

      – Ahmed Abdelhameed
      Nov 20 '18 at 22:03














    • 1





      Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

      – Ahmed Abdelhameed
      Nov 20 '18 at 22:03








    1




    1





    Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

    – Ahmed Abdelhameed
    Nov 20 '18 at 22:03





    Using CDbl will fail if the user input is not a number. You need to validate the user input to make sure it's a number that can be casted to Double. The standard way of doing so is by using TryParse (Double.TryParse in your case) as suggested by LarsTech in the comments above.

    – Ahmed Abdelhameed
    Nov 20 '18 at 22:03













    0














    Use data-binding instead of If else

    and Double.TryParse instead of CDbl for conversions from string to integers

    and & instead of + when combining strings(or String interpolation).




    Option Strict On - always (which you already did)




    Public Class Planet
    Public ReadOnly Property Name As String
    Public ReadOnly Property WeightFactor As Double

    Public Sub New(name As String, weightFactor As Double)
    Me.Name = name
    Me.WeightFactor = weightFactor
    End Sub
    End Class

    ' Create planets and bind it to the ComboBox
    ' Form constructor
    Public Sub New()
    InitializeComponent()

    Dim planets As New List(Of Planet) From
    {
    New Planet("Mercury", 0.378),
    New Planet("Venus", 0.905),
    New Planet("Earth", 1.0),
    New Planet("Mars", 0.379),
    }

    ' Bind planets to combobox
    ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text
    ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value
    ComboBox1.DataSource = planets
    End Sub


    Then your calculation code will look simpler



    Dim personWeight AS Double
    If Double.TryParse(textbox1.Text, personWeight) Then
    ' SelectedItem is instance of selected planet
    Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

    Dim weightOnPlanet = personWeight * planet.WeightFactor
    Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
    End If





    share|improve this answer




























      0














      Use data-binding instead of If else

      and Double.TryParse instead of CDbl for conversions from string to integers

      and & instead of + when combining strings(or String interpolation).




      Option Strict On - always (which you already did)




      Public Class Planet
      Public ReadOnly Property Name As String
      Public ReadOnly Property WeightFactor As Double

      Public Sub New(name As String, weightFactor As Double)
      Me.Name = name
      Me.WeightFactor = weightFactor
      End Sub
      End Class

      ' Create planets and bind it to the ComboBox
      ' Form constructor
      Public Sub New()
      InitializeComponent()

      Dim planets As New List(Of Planet) From
      {
      New Planet("Mercury", 0.378),
      New Planet("Venus", 0.905),
      New Planet("Earth", 1.0),
      New Planet("Mars", 0.379),
      }

      ' Bind planets to combobox
      ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text
      ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value
      ComboBox1.DataSource = planets
      End Sub


      Then your calculation code will look simpler



      Dim personWeight AS Double
      If Double.TryParse(textbox1.Text, personWeight) Then
      ' SelectedItem is instance of selected planet
      Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

      Dim weightOnPlanet = personWeight * planet.WeightFactor
      Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
      End If





      share|improve this answer


























        0












        0








        0







        Use data-binding instead of If else

        and Double.TryParse instead of CDbl for conversions from string to integers

        and & instead of + when combining strings(or String interpolation).




        Option Strict On - always (which you already did)




        Public Class Planet
        Public ReadOnly Property Name As String
        Public ReadOnly Property WeightFactor As Double

        Public Sub New(name As String, weightFactor As Double)
        Me.Name = name
        Me.WeightFactor = weightFactor
        End Sub
        End Class

        ' Create planets and bind it to the ComboBox
        ' Form constructor
        Public Sub New()
        InitializeComponent()

        Dim planets As New List(Of Planet) From
        {
        New Planet("Mercury", 0.378),
        New Planet("Venus", 0.905),
        New Planet("Earth", 1.0),
        New Planet("Mars", 0.379),
        }

        ' Bind planets to combobox
        ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text
        ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value
        ComboBox1.DataSource = planets
        End Sub


        Then your calculation code will look simpler



        Dim personWeight AS Double
        If Double.TryParse(textbox1.Text, personWeight) Then
        ' SelectedItem is instance of selected planet
        Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

        Dim weightOnPlanet = personWeight * planet.WeightFactor
        Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
        End If





        share|improve this answer













        Use data-binding instead of If else

        and Double.TryParse instead of CDbl for conversions from string to integers

        and & instead of + when combining strings(or String interpolation).




        Option Strict On - always (which you already did)




        Public Class Planet
        Public ReadOnly Property Name As String
        Public ReadOnly Property WeightFactor As Double

        Public Sub New(name As String, weightFactor As Double)
        Me.Name = name
        Me.WeightFactor = weightFactor
        End Sub
        End Class

        ' Create planets and bind it to the ComboBox
        ' Form constructor
        Public Sub New()
        InitializeComponent()

        Dim planets As New List(Of Planet) From
        {
        New Planet("Mercury", 0.378),
        New Planet("Venus", 0.905),
        New Planet("Earth", 1.0),
        New Planet("Mars", 0.379),
        }

        ' Bind planets to combobox
        ComboBox1.DisplayMember = "Name" ' Name of the property used for displaying text
        ComboBox1.ValueMember = "WeightFactor" ' Name of the property used as selected value
        ComboBox1.DataSource = planets
        End Sub


        Then your calculation code will look simpler



        Dim personWeight AS Double
        If Double.TryParse(textbox1.Text, personWeight) Then
        ' SelectedItem is instance of selected planet
        Dim planet = DirectCast(ComboBox1.SelectedItem, Planet)

        Dim weightOnPlanet = personWeight * planet.WeightFactor
        Label4.Text = $"Your weight on {planet.Name} is {weightOnPlanet}"
        End If






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 2:14









        FabioFabio

        19.5k22044




        19.5k22044






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401161%2fvb-net-input-string-was-not-in-a-correct-format-using-convert-todouble%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]