4

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 .

3
  • @rafael - show us the code that isn't working Commented Mar 13, 2010 at 23:08
  • How do you do the conversion? Commented Mar 13, 2010 at 23:09
  • What's the user's windows' locale? sometimes when inserting a value like "0,001" in the field won't correctly translate to "0.001" but to "0,001.0" which is invalid. Commented Dec 3, 2013 at 18:17

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok it now runs fine , but the problem is if i used return; it will skip the guy1=new Guy(); which will make the whole Guy class unusable , even if i used a warning message instead of return; it will assign a permanent 0 value to textbox2 , then the program will always give the "Not enough cash" message ... so i must give textbox2 a permanent value , the weird thing is , in runtime the program use it only even if i changed it , it will use the initial value !!
Hey Rafael, no offense intended, but I think you should be looking at a beginning programming book. The answer I gave solves the problem, but you need to get some fundamentals working a bit better :).
@Rafael - You may also want to override the OnClosing() method to catch the case where the user hits Enter and closes the form.
@Rafael & jvenema - Yeah, sounds like you might want to comment out the Guy code and just put message boxes or breakpoints in your constructor and the other methods. Run through the code (or even better, step through it in the debugger) and see when each method is getting executed (and in what sequence). This might make things a lot clearer.
2

This should be fine

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

Double.TryParse Method (String, Double%)

1 Comment

thanks , but i tired this, when i put it in if() as a condition , it returns false , and the conversion is not carried out .
1

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 ",").

2 Comments

The problem is , I want the textbox2 not to hold any value so the user can input the amount of cash they want but When the form1 start it's initialization it will check whether it has a numeric value or not , and it will act according to that ! but i want to leave it empty !!
Ah, OK. Well, you're trying to grab the contents of the field in your form's constructor. This is before the form has even been displayed to the user, and the string you're getting back is probably empty, so you'll always fail to parse a double from that empty string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.