Convert String to Double

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Convert String to Double

    Hello all,

    what am i doing wrong ?? i am reading a value from a combobox and try to parse the value to a double

    VB.NET-Quellcode

    1. Dim dx as double
    2. Dim schritt As String = ""
    3. schritt = cbxSchritte.SelectedItem.ToString
    4. dx = Double.TryParse(schritt, dx)


    i receive as answer -1

    Topic moved from Tipps&Tricks to Sonstige Problemstellungen ~VaporiZed

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „VaporiZed“ ()

    Before you continue, please activate the recommended settings, especially Option Strict On, because the result of Double.TryParse is a Boolean, which tells you, if the parsing was successful.
    dx will be already filled with the parsed double value of cbxSchritte.SelectedItem: this takes place in the parentheses of TryParse (well, actually not, of cource, but it's the only place where you have to use dx beside the declaration)

    btw: Your code is partially german. So, why don't we talk in german? It's a german forum and it seems, that you are able to use german.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    I can try to write in german , but its not my mother language. I am leaving in germany. so excuse me for my mistakes.

    Ich habe mein Fehler gefunden. Du hast recht Try.Parse returns a bollean so i ich habe mit CDbl(value) geändert und hat es funkioniert
    Well, if you prefer english, it's okay. If you feel safer to describe the problem, it's fine. Could prevent misunderstandings.
    Don't use CDbl in this case. You don't need it. In fact I can't imagine, where you want to use it. This should work:

    VB.NET-Quellcode

    1. Dim dx as Double
    2. Dim Schritt = cbxSchritte.SelectedItem.ToString
    3. If Double.TryParse(schritt, dx) Then
    4. MessageBox.Show($"The selected item was {dx}, the half of it is {dx / 2}.")
    5. Else
    6. MessageBox.Show("That was not a number you selected from the list.")
    7. End If
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.

    aTsiri schrieb:

    CDbl(value)
    isn't good, the good way is this:

    VB.NET-Quellcode

    1. Dim dx as double
    2. Dim schritt = cbxSchritte.SelectedItem.ToString
    3. If Not Double.TryParse(schritt, dx) Then
    4. MessageBox("Wrong Text")
    5. dx = 42 ' default value
    6. ' or
    7. ' Return False
    8. End If
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!