Avatar billede king_toga Nybegynder
27. januar 2004 - 16:13 Der er 7 kommentarer og
1 løsning

VB.NET Double.Parse(String) problem

Har en metode:

Private Sub Add(ByVal x As Integer)

        operation = 0
        nytnummer = "ja"

        If x = 1 Then
            LigMed()

        End If

        If Not nummer2.CompareTo("") Then


            temp = Double.Parse(nummer1) + Double.Parse(nummer2)

            nummer1 = temp.ToString

        End If


        TextBox1.Text = nummer1

    End Sub

Under afvikling af programmet opstår følgende fejl:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.



Mine variable har disse værdier, i det øjeblik debuggeren brokker sig:

    TextBox1.Text    "1"    String
    nummer1        ""      String
    nummer2            "1"    String
    temp            0.0    Double

Hvad går der galt ?

Metoden er del af en simpel lommeregner aplikation.
Her findes samme version i java script: http://www.eksperten.dk/spm/436967
Avatar billede alpapa Nybegynder
27. januar 2004 - 17:46 #1
Det må være dit "nummer1" den er galt med, tror der skal være et tal at parse før den vil godtage det.
Avatar billede king_toga Nybegynder
27. januar 2004 - 22:07 #2
Har overvejet det. Gad vide om det er fordi man ikke kan parse en tom string ?
Jeg har desværre ingen mulighed for at give den en værdi selv, så den ikke er tom. Så virker rutinen ikke... bugger !
Avatar billede king_toga Nybegynder
27. januar 2004 - 23:14 #3
Har løst problemet... det havde ikke noget med parse at gøre.

Her er kode til regneren

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private nummer1 As String = ""
    Private nummer2 As String = ""
    Private memory As Double = 0
    Private nytnummer As String = "tom"
    Private operation As Integer = -1


    'hjælpere

    Private tempLængde As Integer = 0
    Private nummer As String = ""
    Private temp As Double = 0
    Private tal1 As Double = 0
    Private tal2 As Double = 0


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents Button8 As System.Windows.Forms.Button
    Friend WithEvents Button9 As System.Windows.Forms.Button
    Friend WithEvents Button10 As System.Windows.Forms.Button
    Friend WithEvents Button_c As System.Windows.Forms.Button
    Friend WithEvents Button_ce As System.Windows.Forms.Button
    Friend WithEvents Button_off As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button_div As System.Windows.Forms.Button
    Friend WithEvents Button_mul As System.Windows.Forms.Button
    Friend WithEvents Button_sub As System.Windows.Forms.Button
    Friend WithEvents Button_add As System.Windows.Forms.Button
    Friend WithEvents Button_dot As System.Windows.Forms.Button
    Friend WithEvents Button_enter As System.Windows.Forms.Button
    Friend WithEvents UserControl11 As Opgave12._1_Sci_Calc.UserControl1
    Friend WithEvents Button_slet As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.Button6 = New System.Windows.Forms.Button
        Me.Button7 = New System.Windows.Forms.Button
        Me.Button8 = New System.Windows.Forms.Button
        Me.Button9 = New System.Windows.Forms.Button
        Me.Button10 = New System.Windows.Forms.Button
        Me.Button_c = New System.Windows.Forms.Button
        Me.Button_ce = New System.Windows.Forms.Button
        Me.Button_off = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button_div = New System.Windows.Forms.Button
        Me.Button_mul = New System.Windows.Forms.Button
        Me.Button_sub = New System.Windows.Forms.Button
        Me.Button_add = New System.Windows.Forms.Button
        Me.Button_dot = New System.Windows.Forms.Button
        Me.Button_enter = New System.Windows.Forms.Button
        Me.UserControl11 = New Opgave12._1_Sci_Calc.UserControl1
        Me.Button_slet = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(248, 144)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(24, 23)
        Me.Button1.TabIndex = 21
        Me.Button1.Text = "1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(280, 144)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(24, 23)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(312, 144)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(24, 23)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "3"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(248, 112)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(24, 23)
        Me.Button4.TabIndex = 3
        Me.Button4.Text = "4"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(280, 112)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(24, 23)
        Me.Button5.TabIndex = 4
        Me.Button5.Text = "5"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(312, 112)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(24, 23)
        Me.Button6.TabIndex = 5
        Me.Button6.Text = "6"
        '
        'Button7
        '
        Me.Button7.Location = New System.Drawing.Point(248, 80)
        Me.Button7.Name = "Button7"
        Me.Button7.Size = New System.Drawing.Size(24, 23)
        Me.Button7.TabIndex = 6
        Me.Button7.Text = "7"
        '
        'Button8
        '
        Me.Button8.Location = New System.Drawing.Point(280, 80)
        Me.Button8.Name = "Button8"
        Me.Button8.Size = New System.Drawing.Size(24, 23)
        Me.Button8.TabIndex = 7
        Me.Button8.Text = "8"
        '
        'Button9
        '
        Me.Button9.Location = New System.Drawing.Point(312, 80)
        Me.Button9.Name = "Button9"
        Me.Button9.Size = New System.Drawing.Size(24, 23)
        Me.Button9.TabIndex = 8
        Me.Button9.Text = "9"
        '
        'Button10
        '
        Me.Button10.Location = New System.Drawing.Point(248, 176)
        Me.Button10.Name = "Button10"
        Me.Button10.Size = New System.Drawing.Size(56, 23)
        Me.Button10.TabIndex = 9
        Me.Button10.Text = "0"
        '
        'Button_c
        '
        Me.Button_c.ForeColor = System.Drawing.Color.Red
        Me.Button_c.Location = New System.Drawing.Point(248, 48)
        Me.Button_c.Name = "Button_c"
        Me.Button_c.Size = New System.Drawing.Size(24, 23)
        Me.Button_c.TabIndex = 10
        Me.Button_c.Text = "C"
        '
        'Button_ce
        '
        Me.Button_ce.ForeColor = System.Drawing.Color.Red
        Me.Button_ce.Location = New System.Drawing.Point(208, 48)
        Me.Button_ce.Name = "Button_ce"
        Me.Button_ce.Size = New System.Drawing.Size(32, 23)
        Me.Button_ce.TabIndex = 11
        Me.Button_ce.Text = "CE"
        '
        'Button_off
        '
        Me.Button_off.Location = New System.Drawing.Point(208, 176)
        Me.Button_off.Name = "Button_off"
        Me.Button_off.Size = New System.Drawing.Size(32, 23)
        Me.Button_off.TabIndex = 12
        Me.Button_off.Text = "Luk"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(176, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(200, 20)
        Me.TextBox1.TabIndex = 13
        Me.TextBox1.Text = ""
        '
        'Button_div
        '
        Me.Button_div.Location = New System.Drawing.Point(280, 48)
        Me.Button_div.Name = "Button_div"
        Me.Button_div.Size = New System.Drawing.Size(24, 23)
        Me.Button_div.TabIndex = 14
        Me.Button_div.Text = "/"
        '
        'Button_mul
        '
        Me.Button_mul.Location = New System.Drawing.Point(312, 48)
        Me.Button_mul.Name = "Button_mul"
        Me.Button_mul.Size = New System.Drawing.Size(24, 23)
        Me.Button_mul.TabIndex = 15
        Me.Button_mul.Text = "*"
        '
        'Button_sub
        '
        Me.Button_sub.Location = New System.Drawing.Point(344, 48)
        Me.Button_sub.Name = "Button_sub"
        Me.Button_sub.Size = New System.Drawing.Size(24, 23)
        Me.Button_sub.TabIndex = 16
        Me.Button_sub.Text = "-"
        '
        'Button_add
        '
        Me.Button_add.Location = New System.Drawing.Point(344, 80)
        Me.Button_add.Name = "Button_add"
        Me.Button_add.Size = New System.Drawing.Size(24, 56)
        Me.Button_add.TabIndex = 17
        Me.Button_add.Text = "+"
        '
        'Button_dot
        '
        Me.Button_dot.Location = New System.Drawing.Point(312, 176)
        Me.Button_dot.Name = "Button_dot"
        Me.Button_dot.Size = New System.Drawing.Size(24, 23)
        Me.Button_dot.TabIndex = 18
        Me.Button_dot.Text = "."
        '
        'Button_enter
        '
        Me.Button_enter.Font = New System.Drawing.Font("Microsoft Sans Serif", 3.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button_enter.Location = New System.Drawing.Point(344, 144)
        Me.Button_enter.Name = "Button_enter"
        Me.Button_enter.Size = New System.Drawing.Size(24, 56)
        Me.Button_enter.TabIndex = 19
        Me.Button_enter.Text = "Enter"
        Me.Button_enter.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'UserControl11
        '
        Me.UserControl11.BackColor = System.Drawing.Color.Black
        Me.UserControl11.ForeColor = System.Drawing.Color.Red
        Me.UserControl11.Location = New System.Drawing.Point(8, 8)
        Me.UserControl11.Name = "UserControl11"
        Me.UserControl11.Size = New System.Drawing.Size(48, 72)
        Me.UserControl11.TabIndex = 20
        '
        'Button_slet
        '
        Me.Button_slet.Location = New System.Drawing.Point(104, 16)
        Me.Button_slet.Name = "Button_slet"
        Me.Button_slet.Size = New System.Drawing.Size(64, 23)
        Me.Button_slet.TabIndex = 23
        Me.Button_slet.Text = "Slet tegn"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = New System.Drawing.Size(632, 446)
        Me.Controls.Add(Me.Button_slet)
        Me.Controls.Add(Me.UserControl11)
        Me.Controls.Add(Me.Button_enter)
        Me.Controls.Add(Me.Button_dot)
        Me.Controls.Add(Me.Button_add)
        Me.Controls.Add(Me.Button_sub)
        Me.Controls.Add(Me.Button_mul)
        Me.Controls.Add(Me.Button_div)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button_off)
        Me.Controls.Add(Me.Button_ce)
        Me.Controls.Add(Me.Button_c)
        Me.Controls.Add(Me.Button10)
        Me.Controls.Add(Me.Button9)
        Me.Controls.Add(Me.Button8)
        Me.Controls.Add(Me.Button7)
        Me.Controls.Add(Me.Button6)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Opacity = 0.8
        Me.Text = "Sci Calc"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button_slet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_slet.Click
        SletTegn(TextBox1.Text)

    End Sub

    Private Sub Button_ce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_ce.Click
        ClearEverything()

    End Sub

    Private Sub Button_c_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_c.Click
        Clear()

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        CheckNummer("7")

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        CheckNummer("8")

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        CheckNummer("9")

    End Sub

    Private Sub Button_div_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_div.Click
        Divide(1)

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        CheckNummer("4")

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        CheckNummer("5")

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        CheckNummer("6")

    End Sub

    Private Sub Button_mul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_mul.Click
        Multiply(1)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CheckNummer("1")

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        CheckNummer("2")

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        CheckNummer("3")

    End Sub

    Private Sub Button_sub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_sub.Click
        Substract(1)

    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        CheckNummer("0")

    End Sub

    Private Sub Button_dot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_dot.Click
        CheckNummer(".")

    End Sub

    Private Sub Button_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_add.Click
        Add(1)

    End Sub

    Private Sub Button_enter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_enter.Click
        LigMed()

    End Sub

    Private Sub Button_off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_off.Click
        Me.Close()

    End Sub





    Private Sub Display(ByVal nummer As Object)
        TextBox1.Text = nummer
    End Sub

    Private Sub Clear()
        nummer1 = ""
        nummer2 = ""
        nytnummer = "tom"
        TextBox1.Text = ""
    End Sub

    Private Sub SletTegn(ByVal input As String)
        tempLængde = input.Length
        input = input.Substring(0, tempLængde - 1)

        If Not nummer2.CompareTo("") Then
            nummer2 = input
            Display(nummer2)

        Else
            nummer1 = input
            Display(nummer1)

        End If

    End Sub

    Private Sub ClearEverything()
        nummer2 = ""
        nytnummer = "tom"
        TextBox1.Text = ""

    End Sub

    Private Function CheckNummer(ByVal input As String) As String
        'If input.CompareTo(".") Then
        'nummer = input

        ' If Not nummer.IndexOf(".") = -1 Then
        ' input = ""

        ' End If
        ' End If

        If nytnummer.CompareTo("ja") = 0 Then
            nummer2 = String.Concat(nummer2, input)
            Display(nummer2)

        Else
            If nytnummer.CompareTo("tom") = 0 Then
                nummer1 = input
                nummer2 = ""
                nytnummer = "nej"
            Else
                nummer1 = String.Concat(nummer1, input)

            End If
            Display(nummer1)

        End If

        Return nummer1

    End Function

    Private Sub Add(ByVal x As Integer)

        If x = 1 Then
            LigMed()
        End If

        If Not nummer2.CompareTo("") = 0 Then
            Try
                temp = Double.Parse(nummer1) + Double.Parse(nummer2)
            Catch ex As Exception

            End Try

            nummer1 = temp.ToString
        End If

        TextBox1.Text = nummer1
        operation = 0
        nytnummer = "ja"

    End Sub

    Private Sub Substract(ByVal x As Integer)
        If x = 1 Then
            LigMed()
        End If

        If Not nummer2.CompareTo("") = 0 Then
            nummer1 = Double.Parse(nummer1) - Double.Parse(nummer2)
        End If

        nytnummer = "ja"
        operation = 1
        TextBox1.Text = nummer1
    End Sub

    Private Sub Multiply(ByVal x As Integer)
        If x = 1 Then
            LigMed()
        End If

        If Not nummer2.CompareTo("") = 0 Then
            nummer1 = Double.Parse(nummer1) * Double.Parse(nummer2)
        End If

        nytnummer = "ja"
        operation = 2
        TextBox1.Text = nummer1
    End Sub

    Private Sub Divide(ByVal x As Integer)
        If x = 1 Then
            LigMed()
        End If

        If Not nummer2.CompareTo("") = 0 Then
            nummer1 = Double.Parse(nummer1) / Double.Parse(nummer2)
        End If

        nytnummer = "ja"
        operation = 3
        TextBox1.Text = nummer1
    End Sub

    Private Sub LigMed()

        If operation = 0 Then
            Add(0)

        ElseIf operation = 1 Then
            Substract(0)

        ElseIf operation = 2 Then
            Multiply(0)

        ElseIf operation = 3 Then
            Divide(0)

        End If


        nummer2 = ""
        operation = -1

    End Sub
Avatar billede alpapa Nybegynder
27. januar 2004 - 23:26 #4
Okay - jamen, det var da godt du fik det til at spille ;-)
Avatar billede king_toga Nybegynder
27. januar 2004 - 23:35 #5
Jir

fejlen var:

String.compareTo(string) returnerer et tal og IKKE en bool'sk som java's compareTo() gør
Avatar billede arne_v Ekspert
27. januar 2004 - 23:40 #6
java.lang.String compareTo(String) returnerer altså int
Avatar billede alpapa Nybegynder
27. januar 2004 - 23:41 #7
okay
Avatar billede king_toga Nybegynder
27. januar 2004 - 23:48 #8
jeg er vist ved at være lidt kogt... huskeren spiller mig et puds - jeg er ude af her !!! :)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester