Hey guys,

I'm trying to make an Area & Perimeter calculator, and I've run into some issues.
Everything is working, except for the very end of the code I seem to be getting an error when it comes to converting the decimal variables back to string.

Here is my code, let me know if you need to know anything else:

namespace AreaPerimeterCalculator
{
    public partial class frmAreaPerimeter : Form
    {
        public frmAreaPerimeter()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //This states the variables.
            decimal length = 0;
            decimal width = 0;
            decimal area = 0;
            decimal perimeter = 0;


            area = CalculateArea(length, width);
            perimeter = CalculatePerimeter(length, width);





            //INPUT
            //This occurs when the values have been inputted correctly.
            try
            {

                length = Convert.ToDecimal(txtLength.Text);
                width = Convert.ToDecimal(txtWidth.Text);
            }

            //This occurs when the value has been inputted incorrectly.
            catch (Exception)
            {
                MessageBox.Show("You have entered an incorrect value, please enter numbers only.");
                txtLength.Focus();
            }
        }






        //PROCESS

        private decimal CalculateArea(decimal length, decimal width)
        {//start CalculateArea

            decimal area;
            area = length * width;

            return area;
        }end CalculateArea

        

        private decimal CalculatePerimeter(decimal length, decimal width)
        {//start CalulatePerimeter
            decimal perimeter;
            perimeter = 2 * length + 2 * width;

            return perimeter;
        }//end CalulatePerimeter

       



        //OUTPUT

        //This converts the numerical value into text form, then outputs the correct data.
        txtArea.Text = Convert.ToString(area);
        txtPerimeter.Text = Convert.ToString(perimeter);
        



        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}

Recommended Answers

All 2 Replies

Lines 76 and 77 aren't part of any method. Put them in a method.

Lines 76 and 77 aren't part of any method. Put them in a method.

Thank you! I just had to put them in the btnCalculate_Click method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.