rafael
rafael

Reputation: 687

Input string was not in a correct format. Converting String to double

I'm quite a beginner in C# , empty string to double conversion can be carried out under the button1_click event ..but doing it under Public Form1() it gives me this error

Input string was not in a correct format.

Here`s the code..(the form1.cs and the Guy.cs class)

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;

 namespace WindowsFormsApplication1
 {
     public partial class Form1 : Form
     {
        public Form1()
        {
            InitializeComponent();

            guy1 = new Guy() ;
            guy1.guyname = textBox1.Text;                
            guy1.guycash = double.Parse(textBox2.Text);
        }                 

    }

    Guy guy1 ;

    private void button1_Click(object sender, EventArgs e)
    {

        label5.Text = guy1.TakeCash(double.Parse(textBox3.Text)).ToString();

    }
 }


}

The Guy.cs Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Guy
    {
        private string name;
        private double cash;

        public string guyname
        {
            get { return name; }
            set { name = value; }
        }

        public double guycash
        {
            get { return cash ; }
            set { cash = value; }
        }



        public double TakeCash(double amount)
        {
            if (cash > amount)
            {
                cash -= amount;
                return cash;
            }
            else
            {
                MessageBox.Show("Not enough Cash.");
                return 0;
            }
        }


    }
}

the error is caused by the guy1.guycash = double.Parse(textBox2.Text); line , when i try double.TryParse(textbox2.Text, out x) in If() before it, it returns false .

how to solve this please ? thanks in advance .

Upvotes: 4

Views: 50550

Answers (3)

Andrew Bezzub
Andrew Bezzub

Reputation: 16032

It looks like the problem is that you are not handling inproper user input. You are trying to parse string from textbox to double without suggestion that it may not be parsed ok (for example user could input 'abcd' in the textbox). Your code should use TryParse method and show error message when input was not parsed.

I guess parsing fails because of non-numeric input or becase of culture problems (like you have "." as desimal symbol by user inputs number with ",").

Upvotes: 1

Jerod Venema
Jerod Venema

Reputation: 44632

Continuing from astanders answer:

double d;
if(!Double.TryParse(textBox2.Text, out d)){
    return; // or alert, or whatever.
}

guy1 = new Guy() ;
guy1.guyname = textBox1.Text;                
guy1.guycash = d;

What you're doing is attempting the parse, and if it fails, taking some other action. Since the user can input anything they want, this guarantees that if you fail to parse the input (because it's not a decimal), you can handle it nicely and tell them to fix their input.

Upvotes: 4

Adriaan Stander
Adriaan Stander

Reputation: 166326

This should be fine

double d;
Double.TryParse(String.Empty, out d);

Double.TryParse Method (String, Double%)

Upvotes: 2

Related Questions