Temperature Converter

Keep getting a error, operator cannot be applied

Page 1 of 1

6 Replies - 5620 Views - Last Post: 16 September 2010 - 05:18 PM Rate Topic: -----

#1 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Temperature Converter

Posted 16 September 2010 - 02:57 PM

Hi,
I am completely new at writing code, and I want to make a application that converts Fahrenheit to Celsius using Windows Forms and C#. I have been working on this for over a week and cannot even get to the debugging stage because I keep getting errors. I finally have it down to only two errors and cannot find any answers that help. I am really frustrated with this and was wondering what I was doing wrong and why I keep getting this error message. It tells me that the operator cannot be applied because of a .int. I have posted my code down below, to show what I have done already. Also, will someone please explain why this is happening, I read that I had to convert it to string. But the way I have tried to do that doesn't work. Please help.

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();
        }

        private void Fahrenheit_Click(object sender, EventArgs e)
        {
          
           double dblCelsius;
           double dblFahrenheit;

            dblCelsius = double.Parse(Fahrenheit.Text);
            dblFahrenheit = (Celsius * 9 / 5) + 32;
            Celsius.Text= Convert.ToString(dblCelsius);
            
            
        }

        private void Celsius_Click(object sender, EventArgs e)
        {
            double dblFahrenheit;
            double dblCelsius;

            dblFahrenheit = double.Parse(Celsius.Text);
            dblCelsius = (Fahrenheit - 32) * 5 / 9;
            Fahrenheit.Text = Convert.ToString(dblFahrenheit);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 16 September 2010 - 03:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Converter

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Temperature Converter

Posted 16 September 2010 - 03:05 PM

Please copy and paste the errors exactly as they appear in Visual Studio.
Was This Post Helpful? 0
  • +
  • -

#3 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Re: Temperature Converter

Posted 16 September 2010 - 03:24 PM

InitializeComponent();
        }

        private void Fahrenheit_Click(object sender, EventArgs e)
        {
          
           double dblCelsius;
           double dblFahrenheit;

            dblCelsius = double.Parse(Fahrenheit.Text);
            dblFahrenheit = (Celsius * 9 / 5) + 32;       ////This is error 1////
            Celsius.Text= Convert.ToString(dblCelsius);
            
            
        }

        private void Celsius_Click(object sender, EventArgs e)
        {
            double dblFahrenheit;
            double dblCelsius;

            dblFahrenheit = double.Parse(Celsius.Text);
            dblCelsius = (Fahrenheit - 32) * 5 / 9;     ////This is error 2////
            Fahrenheit.Text = Convert.ToString(dblFahrenheit);
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }

The errors read Button Form1.Celsius
Operator “*” cannot be applied to operands of the type ‘System.Windows.Forms.Buttons.’ and ‘int.’ For the first error in //// //// above.
And Button Form2.Fahrenheit
Operator “-” cannot be applied to operands of the type ‘System.Windows.Forms.Buttons.’
For the second error.
Thank You for your help.

MOD EDIT: Again, when posting code....USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 16 September 2010 - 03:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Temperature Converter

Posted 16 September 2010 - 03:33 PM

It looks like the variables Fahrenheit and Celsius are the names of Buttons in your form. You should be using dblFahrenheit and dblCelsius, the variables you just created in the functions and parsed the text data into.
Was This Post Helpful? 0
  • +
  • -

#5 eric4215  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 09-September 10

Re: Temperature Converter

Posted 16 September 2010 - 04:18 PM

I can't believe i missed that!, It still doesn't work, but all the errors are gone, Thank You.
Now I have a FormatException was unhandled from this line dblFahrenheit = double.Parse(Celsius.Text);
Was This Post Helpful? 0
  • +
  • -

#6 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Temperature Converter

Posted 16 September 2010 - 04:42 PM

You might have an invalid value in Celsius.Text. Use Double.TryParse instead
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Temperature Converter

Posted 16 September 2010 - 05:18 PM

Also, like I said Fahrenheit and Celsius appear to be the names of Button controls, not TextBox controls. Verify the name of the TextBox controls from which you're getting the data.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1