2

I am new to c# , i have read few basic concepts and now want to learn actual programming , thats why i started out with simple Calculator program

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

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

        double num1 = 0;
        double num2 = 0;


        private void one_Click(object sender, EventArgs e)
        {
            textBox1.Text =textBox1.Text+one.Text;
        }

        private void clear_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
           // textBox1.Text = " ";
        }

        private void two_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text+two.Text;
        }

        private void three_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text+three.Text;
        }

        private void four_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text+four.Text;
        }

        private void five_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + five.Text;
        }

        private void six_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + six.Text;
        }

        private void seven_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + seven.Text;
        }

        private void eight_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + eight.Text;
        }

        private void nine_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + nine.Text;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void add_Click(object sender, EventArgs e)
        {
            num1 = num1 + double.Parse(textBox1.Text) ;
            textBox1.Clear();

        }

        private void ten_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + ten.Text;
        }

        private void equal_Click(object sender, EventArgs e)
        {
            num2 = num1 + double.Parse(textBox1.Text) ;
            textBox1.Text = num2.ToString();
            num1 = 0;

        }


    }
}

This code is given in the book and they said its working .Actual problem is it works ONLY some times and some times it gives error as " Input string was not in a correct format " What is the solution . My second question is -- is this right approach to learn any programming language ? or should i just continue reading some more stuff n programs ? If some one can help me with websites containing sample programs it will be very helpful .

  • There is no validation in this code, so if the user puts something in a text box that cannot be resolved to a double then it will generate this error. I guess the program works if the user puts in the right data. – Colin Mackay Jun 26 '13 at 11:35

6 Answers6

1

Your problem is parsing the double. The Parse function cannot parse a string that does not represent a double.

Consider the string "135.42", this could easily be parsed to the double value 135.42. But what if the string was "Abc#%%@". This does not represent a double value and that is where you are getting your exception.

If this is your first programming language, you should read a introductory book to C# as a new programmer.

Justin
  • 6,373
  • 9
  • 46
  • 72
  • additional info , user can enter numbers only so no characters are allowed to generate that kind of exception . its working for few numbers properly eg 12+12 or 123+123 is executing properly but 56+56 is showing that exception – johny liver Jun 26 '13 at 12:20
1

If double.Parse can't parse the string into a double then an exception is thrown. However, it's often better to use double.TryParse as it will simply return false instead of throwing an exception.

double tmp;
if(double.TryParse(textBox1.Text, out tmp))
{   
   num2 = num1 + tmp;
   textBox1.Text = num2.ToString();
   num1 = 0;
}
else
{
    //double could not be parsed from textbox
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
1

You need to restrict the user to input only numbers in your textbox. You can do it by using keydown event of textbox. Where you will check if inputed char was numeric, only then keep it in the textbox. Here is the solution of your problem. Have a look at this:

How do I make a textbox that only accepts numbers?

For learning C#, i would prefer:

C# 5.0 in a Nutshell: The Definitive Reference

Community
  • 1
  • 1
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • on the scale of 1-10 how many points will you give to this book ? I am really interested in learning c# so if its good i will buy it tomorrow only. – johny liver Jun 26 '13 at 12:12
  • @johnyliver I will give it 10 out of 10. Tghis is the best book for every C# programmer beginner, intermediate or advance level. Try it online first here, then buy it http://my.safaribooksonline.com/book/programming/csharp/9781449334192 – Shaharyar Jun 26 '13 at 15:00
0

Here is the magic: double.Parse(textBox1.Text) This function accepts only numbers (with comma or dot depends on your culture) in the string. If you pass anything else you will receive the given exception. Validate the input before you parse it or Use TryParse

double valueinDouble;
var success = double.TryParse(textBox1.Text), out valueinDouble)
if (success)
{
    // do the calculation here
}

The TryParse has another implementation where you can specify the culture.

 var success = double.TryParse(textBox1.Text), NumberStyles.Any, new CultureInfo("en-US"), out valueinDouble)
speti43
  • 2,886
  • 1
  • 20
  • 23
  • i have used validation in design part of the program to enter numbers only . so user cant enter characters , still i will try your code .Thanks for help – johny liver Jun 26 '13 at 12:15
0

Before you do double.Parse() you should verify the user input, i.e. That it is an actual number.

I would suggest using TryParse method instead, as such:

double num;

bool isNum = double.TryParse(Str, out num);

if (isNum)
{
//Calculate here
}
Marcus
  • 8,230
  • 11
  • 61
  • 88
0

If you wanted to handle invalid arguments more gracefully you could use Double.TryParse instead.

rie819
  • 1,249
  • 12
  • 19