none
Parsing to double RRS feed

  • Question

  • I am a beginner to C#.net and programming for "Calculator". I am facing problems while parsing. Kindly help. My code is given below:

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

    namespace Calculator1
    {
        public partial class Form1 : Form
        {

            double total1 = 0;
            double number2 = 0;
            double answer = 0;

            string theOperator;
                         
            public Form1()

            {
                
                InitializeComponent();
            }
            
            
            private void btnOne_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnOne.Text;
            }

            private void btnTwo_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
            }

            private void btnThree_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnThree.Text;
            }

            private void btnFour_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnFour.Text;
            }

            private void btnFive_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnFive.Text;
            }

            private void btnSix_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnSix.Text;
            }

            private void btnSeven_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
            }

            private void btnEight_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnEight.Text;
            }

            private void btnNine_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnNine.Text;
            }

            private void btnZero_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnZero.Text;
            }

            private void btnPoint_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
            }

            private void btnClear_Click(object sender, EventArgs e)
            {
                 txtDisplay.Clear();
            }
           
            private void btnPlus_Click(object sender, EventArgs e)
            {
                try
                {
                    total1 = total1 + double.Parse(txtDisplay.Text);
                }
                catch (System.Exception excep)
                {
                    MessageBox.Show(excep.GetType().ToString());
                }
                theOperator = "+";
                txtDisplay.Clear();
            }
            private void btnMinus_Click(object sender, EventArgs e)
            {
                try
                {
                    total1 = total1 + double.Parse(txtDisplay.Text);
                }
                catch (System.Exception excep)
                {
                    MessageBox.Show(excep.GetType().ToString());
                } 
                theOperator = "-";
                txtDisplay.Clear();
            }

            private void btnEquals_Click(object sender, EventArgs e)
            {
           
                try
                {
                    number2 = double.Parse(txtDisplay.Text);
                }
                catch (System.Exception excep)
                {
                    MessageBox.Show(excep.GetType().ToString());
                }
                
                switch (theOperator)
                {
                    case "+":    
                        answer = total1 + number2;
                        txtDisplay.Text = answer.ToString();
                        total1 = 0;
                        break;
                    case "-" :
                        answer = total1 - number2;
                        txtDisplay.Text = answer.ToString();
                        total1 = 0;
                        break;
                    default:
                        answer = 0;
                        MessageBox.Show ("Answer = 0");
                        break;
                 }
            }

           
        }
    }

     
    Sunday, June 15, 2014 6:48 AM

Answers

  • Hi Abdul,

    You can use Dobule.TryParse method. Code may be something like below.

    string incoming  = "22.5";
    
                double result = 0;
    
                bool flag = double.TryParse(incoming, out result);
    
                if (flag)
    
                {
    
                    // covnert successfully
    
                }
    
                else {
    
                    //convert failed
    
                }
    

    http://msdn.microsoft.com/en-us/library/994c0zb1.aspx.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 16, 2014 5:24 AM
    Moderator