MH0st
MH0st

Reputation: 3

C# Temperature Conversion Form

I have this form to due for a school class i am in. Im having trouble running the program. Here are the requirements:

This is the code I have:

namespace TemperatureConversionForm
{
    public partial class frmTemperatureConverter : Form
    {
        public frmTemperatureConverter()
        {
            InitializeComponent();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            double tempurature;
            double celsius;
            double fahrenheit;

            if (!double.TryParse(txtInput.Text, out tempurature) == false )
            {
                MessageBox.Show("ERROR: Please enter a numeric temperature to convert");
                txtInput.ResetText();
                txtInput.Focus();
            }

            if (rdoCelsius.Checked == true)
            {
                celsius = double.Parse(txtInput.Text);
                celsius = (5.0 / 9.0 * (txtInput - 32));
                lblConversion.Text = celsius.ToString();
            }
            else if (rdoFahrenheit.Checked == true)
            {
                fahrenheit = double.Parse(txtInput.Text);
                fahrenheit = (txtInput * 9.0 / 5.0 + 32);
                lblConversion.Text = fahrenheit.ToString();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtInput.ResetText();
            lblConversion.ResetText();

            txtInput.Focus();
        }

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

I am getting 2 errors:

Code Description

CS0019 Operator '-' cannot be applied to operands of type 'TextBox' and 'Int'

CS0019 Operator '*' cannot be applied to operands of type 'TextBox' and 'double'

Upvotes: 0

Views: 3863

Answers (1)

David
David

Reputation: 219037

Just as the compiler is telling you, you're trying to perform math on a TextBox object:

txtInput - 32

It's likely that you meant to perform math on the value that you parsed from the TextBox:

celsius = (5.0 / 9.0 * (celsius - 32));

and:

fahrenheit = (fahrenheit * 9.0 / 5.0 + 32);

Basically, a TextBox is an object, not a value. It has properties and functionality and all sorts of things that objects have. While you may be tempted to conceptually think of it as representing the value that it holds, the language needs you to be more specific than that. You get the value from the TextBox here:

celsius = double.Parse(txtInput.Text);

Then you need to perform calculations on that value itself, not on the TextBox object from which it came.

Upvotes: 1

Related Questions