10 Replies - 849 Views - Last Post: 04 April 2010 - 08:01 AM Rate Topic: -----

#1 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Method and addition issue

Posted 03 April 2010 - 05:46 AM

Hello,

I'm having a couple of issues with a program that I am coding. It's supposed to be a simple temperature converter, which takes a Fahrenheit temp (user input) and converts it to Centigrade and Kelvin in two separate labels.

First, I get an error in my button's click event when I attempt to call to my two methods. "No overload for method 'INSERT METHOD NAME HERE' takes '0' arguments".

Second, I get an error for my first method: "Not all code paths return a value".

Third, the formula for converting to Kelvin, in the second method, gives me an error: "Operator '+' cannot be applied to operands of type 'decimal' and 'double'". How do you do addition for decimals and doubles then?

Here is my code, that includes the button and the two methods.

 private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = decimal.Parse(txtTempFahrenheit.Text);
                
                convertCentigrade();
                convertKelvin();
            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        public decimal convertCentigrade(decimal tempCentigrade)
        {
            //Perform the math for converting F to C
            tempCentigrade = (tempFahrenheit - 32) * 5 / 9;
            lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
                      

        }            

        public decimal convertKelvin(decimal tempKelvin)
        {
            //Perform the math for converting from F to K
            tempKelvin = (tempFahrenheit + 459.67) * 5 / 9;
            lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");


        } 



Any insights would be most appreciated. I look forward to your responses, and hope that my issues have simple fixes. Thanks.

Nathan

Is This A Good Question/Topic? 0
  • +

Replies To: Method and addition issue

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Method and addition issue

Posted 03 April 2010 - 06:18 AM

public decimal convertCentigrade(decimal tempCentigrade)
{
   //Perform the math for converting F to C
   tempCentigrade = (tempFahrenheit - 32) * 5 / 9;
   lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
}


The errors are very clear if you just read them.
You've declared that function as taking a single decimal value as an argument and returning a decimal value.

First:

Quote

No overload for method 'INSERT METHOD NAME HERE' takes '0' arguments

You're making your call here:
convertCentigrade();

How many arguments are you passing?

Second: Where are you returning a value?
Was This Post Helpful? 0
  • +
  • -

#3 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 03 April 2010 - 07:50 AM

Ok, I think I figured out what my problems were in regards to the methods themselves.

  convertCentigrade(tempFahrenheit);
  convertKelvin(tempFahrenheit);



And as the methods did not actually need to return a value, since part of each method is displaying the result of the math in the labels, I made them void.

 public void convertCentigrade(decimal tempFahrenheit)
        {
            //Perform the math for converting F to C
            tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
                      

        }  



The only remaining error is the one that is actually most confusing to me. The one that says "Operator '+' cannot be applied to operands of type 'decimal' and 'double'". This error is in the method "convertKelvin".

public void convertKelvin(decimal tempFahrenheit)
        {
            //Perform the math for converting from F to K
            tempKelvin = (tempFahrenheit + 459.67) * (5 / 9);
            lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");


        }



I am attempting to add 459.67 to the value of the variable "tempFahrenheit". I'm not sure why the compiler does not like that number.

Again, thanks for any assistance.

Nathan
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Method and addition issue

Posted 03 April 2010 - 09:28 AM

So you would need to use one of the members of the System.Convert class to coerce types.
Was This Post Helpful? 0
  • +
  • -

#5 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 03 April 2010 - 03:54 PM

Okay, so I finally figured out the problem with my Kelvin conversion formula. That is no longer giving me an error. The problem I'm having now is that when I run the program, the result I get from each conversion is "0.00". I am either not calling the methods correctly, or the methods aren't doing anything. I've been recoding, trying different syntax to get this to work, with no luck. Here is my code as it stands now:

public partial class frmUnit06project : Form
    {
        //Declare variable
        decimal tempFahrenheit, tempCentigrade, tempKelvin;
        public frmUnit06project()
        {
            InitializeComponent();
            //Display a message with simple instruction upon startup of program
            MessageBox.Show("Enter a Farhrenheit temperature to be converted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        

        private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = decimal.Parse(txtTempFahrenheit.Text);
                
                //convertCentigrade(tempFahrenheit);
                //convertKelvin(tempFahrenheit);
                lblOutputCentigrade.Text = convertCentigrade(tempCentigrade).ToString("N");
                lblOutputKelvin.Text = convertKelvin(tempKelvin).ToString("N");
                

            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        private decimal convertCentigrade(decimal tempFahrenheit)
        {
            //Perform the math for converting F to C
            //tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            //return tempCentigrade;
            //lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
            return tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
                      

        }            

        private decimal convertKelvin(decimal tempFahrenheit)
        {
            //Perform the math for converting from F to K
            //tempKelvin = (tempFahrenheit + 459.67m) * (5 / 9);
            //return tempKelvin;
            //lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");
            return tempKelvin = (tempFahrenheit + 459.67m) * (5 / 9);
            

        } 



As I said, the program runs, but doesn't produce any results. As you can see from the lines of code that are commented out, I'm trying several different ways but it is still not working properly. Again, thanks for any advice.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Method and addition issue

Posted 03 April 2010 - 05:04 PM

I've never used decimal before; is there a reason you're not using double? It would seem the natural datatype to be used in this scenario.
Was This Post Helpful? 0
  • +
  • -

#7 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 03 April 2010 - 05:07 PM

Hmm, I guess there is no reason why I'm not using double. I guess I had the notion in my head that I needed the results to display out to 2 decimal places, thus I was using decimal. I will recode with double and see if this fixes it.
Was This Post Helpful? 0
  • +
  • -

#8 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 03 April 2010 - 05:20 PM

Well, I recoded using double, but that didn't change anything as far as the results I'm getting for my labels. It has to have something to do with the way I am calling the methods, or with the methods themselves. I'm really confused as to what they problem is though. I never had this kind of issue with VB.

 public partial class frmUnit06project : Form
    {
        //Declare variables
        double tempFahrenheit, tempCentigrade, tempKelvin;
        public frmUnit06project()
        {
            InitializeComponent();
            //Display a message with simple instruction upon startup of program
            MessageBox.Show("Enter a Farhrenheit temperature to be converted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        

        private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = double.Parse(txtTempFahrenheit.Text);
                
                //convertCentigrade(tempFahrenheit);
                //convertKelvin(tempFahrenheit);
                //lblOutputCentigrade.Text = convertCentigrade(tempFahrenheit).ToString("N");
                //lblOutputKelvin.Text = convertKelvin(tempFahrenheit).ToString("N");
                lblOutputCentigrade.Text = tempCentigrade.ToString("N");
                lblOutputKelvin.Text = tempKelvin.ToString("N");

            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        private double convertCentigrade(double tempFahrenheit)
        {
            
            //Perform the math for converting F to C
            //tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            //return tempCentigrade;
            //lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
            return tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
                      

        }            

        private double convertKelvin(double tempFahrenheit)
        {
            
            //Perform the math for converting from F to K
            //tempKelvin = (tempFahrenheit + 459.67m) * (5 / 9);
            //return tempKelvin;
            //lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");
            return tempKelvin = (tempFahrenheit + 459.67) * (5 / 9);
            

        }


Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6052
  • View blog
  • Posts: 23,487
  • Joined: 23-August 08

Re: Method and addition issue

Posted 03 April 2010 - 05:26 PM

Well, according to that code... you AREN'T calling your functions at all.
Was This Post Helpful? 0
  • +
  • -

#10 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 03 April 2010 - 05:39 PM

Yes, that was an oops brought on by the multiple attempts that I have made to get this to work. Without commented-out lines of code as such:

public partial class frmUnit06project : Form
    {
        //Declare variables
        double tempFahrenheit, tempCentigrade, tempKelvin;
        public frmUnit06project()
        {
            InitializeComponent();
            //Display a message with simple instruction upon startup of program
            MessageBox.Show("Enter a Farhrenheit temperature to be converted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        

        private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = double.Parse(txtTempFahrenheit.Text);
                
                convertCentigrade(tempFahrenheit);
                convertKelvin(tempFahrenheit);
                //lblOutputCentigrade.Text = convertCentigrade(tempFahrenheit).ToString("N");
                //lblOutputKelvin.Text = convertKelvin(tempFahrenheit).ToString("N");
                lblOutputCentigrade.Text = tempCentigrade.ToString("N");
                lblOutputKelvin.Text = tempKelvin.ToString("N");

            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        private double convertCentigrade(double tempFahrenheit)
        {
            
            //Perform the math for converting F to C
            //tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            //return tempCentigrade;
            //lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
            return tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
                      

        }            

        private double convertKelvin(double tempFahrenheit)
        {
            
            //Perform the math for converting from F to K
            //tempKelvin = (tempFahrenheit + 459.67m) * (5 / 9);
            //return tempKelvin;
            //lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");
            return tempKelvin = (tempFahrenheit + 459.67) * (5 / 9);
            

        } 



It still does not work.

When I change the code around as follows:

 public partial class frmUnit06project : Form
    {
        //Declare variables
        double tempFahrenheit, tempCentigrade, tempKelvin;
        public frmUnit06project()
        {
            InitializeComponent();
            //Display a message with simple instruction upon startup of program
            MessageBox.Show("Enter a Farhrenheit temperature to be converted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        

        private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = double.Parse(txtTempFahrenheit.Text);
                
                //convertCentigrade(tempFahrenheit);
               // convertKelvin(tempFahrenheit);
                lblOutputCentigrade.Text = convertCentigrade(tempFahrenheit).ToString("N");
                lblOutputKelvin.Text = convertKelvin(tempFahrenheit).ToString("N");
                //lblOutputCentigrade.Text = tempCentigrade.ToString("N");
               // lblOutputKelvin.Text = tempKelvin.ToString("N");

            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        private double convertCentigrade(double tempFahrenheit)
        {
            
            //Perform the math for converting F to C
            //tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            //return tempCentigrade;
            //lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
            return (tempFahrenheit - 32) * (5 / 9);
                      

        }            

        private double convertKelvin(double tempFahrenheit)
        {
            
            //Perform the math for converting from F to K
            //tempKelvin = (tempFahrenheit + 459.67m) * (5 / 9);
            //return tempKelvin;
            //lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");
            return (tempFahrenheit + 459.67) * (5 / 9);
            

        } 



It also does not work.

Both ways the program runs, but it is giving an output of 0.00 for both labels. The second code example is similar to an example provided in our text, which is very vague. Again, thanks for any advice.
Was This Post Helpful? 0
  • +
  • -

#11 nsanf0rd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 02-April 10

Re: Method and addition issue

Posted 04 April 2010 - 08:01 AM

Here is yet another change in the code, with still no luck. With this code I'm getting errors for tempCentigrade and tempKelvin that state "never assigned to, and will always have its default value 0". As stated previously, the program runs, it just does not produce any result (other than 0.00), so I think the problem is either in the way that I am calling on the methods or in the methods themselves. I'm just not seeing it.

public partial class frmUnit06project : Form
    {
        //Declare variables
        double tempFahrenheit, tempCentigrade, tempKelvin;
        public frmUnit06project()
        {
            InitializeComponent();
            //Display a message with simple instruction upon startup of program
            MessageBox.Show("Enter a Farhrenheit temperature to be converted.", "Instructions", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

        

        private void btnConvertTemperature_Click(object sender, EventArgs e)
        {
            //Convert the Fahrenheit temperature to both Centigrade and Kelvin and
            //display the results in the two corresponding labels
            
            try
            {
                //Attempt to extract numeric data from the textbox and call the conversion methods
                tempFahrenheit = double.Parse(txtTempFahrenheit.Text);
                
                
                convertCentigrade(tempFahrenheit, tempCentigrade);
                convertKelvin(tempFahrenheit, tempKelvin);
                //lblOutputCentigrade.Text = convertCentigrade(tempFahrenheit).ToString("N");
                //lblOutputKelvin.Text = convertKelvin(tempFahrenheit).ToString("N");
                lblOutputCentigrade.Text = tempCentigrade.ToString("N");
                lblOutputKelvin.Text = tempKelvin.ToString("N");

            }
            catch
            {
                //If the try block fails display an error message, clear and reset focus to the textbox
                MessageBox.Show("Temperature must be in numeric format.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTempFahrenheit.Clear();
                txtTempFahrenheit.Focus();
            }
        }

        private double convertCentigrade(double tempFahrenheit, double tempCentigrade)
        {
            
            //Perform the math for converting F to C
            tempCentigrade = (tempFahrenheit - 32) * (5 / 9);
            //return tempCentigrade;
            //lblOutputCentigrade.Text = decimal.Round(tempCentigrade, 2).ToString("N");
            //return (tempFahrenheit - 32) * (5 / 9);
            return tempCentigrade;        

        }            

        private double convertKelvin(double tempFahrenheit, double tempKelvin)
        {
            
            //Perform the math for converting from F to K
            tempKelvin = (tempFahrenheit + 459.67) * (5 / 9);
            //return tempKelvin;
            //lblOutputKelvin.Text = decimal.Round(tempKelvin, 2).ToString("N");
            //return (tempFahrenheit + 459.67) * (5 / 9);
            return tempKelvin;

        }



Again, thanks for any advice. This is really frustrating because I'm sure its some simple mistake that I'm not catching.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1