11 Replies - 771 Views - Last Post: 12 May 2010 - 02:05 PM Rate Topic: -----

#1 Munkybones  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 31-October 09

calling a method

Posted 08 May 2010 - 10:33 AM

ok i have been racking my brain trying to figure this out..and for some reason i cant quite get it to display. I am writing a method to make a calculation and return a value inside an if statement to a label i already have set up for the display text.
This is the method i have set up to return the value of farenheit converted to celsius

private double converttocelsius(double farenheit)
        {
            
             
            
            double convcelsius = farenheit * 1.8 + 32;
            return convcelsius;
        }



and this is what i have come up with thus far trying to come up with the solution. But im getting an error about converting from double to string and for invalid arguements

private void conversionbtn_Click(object sender, EventArgs e)
        {
            string celsius;
            string farenheit;
            double convcelsius;
            double convfarenheit;
            celsius = celsiusinput.Text;
            farenheit = farenheitinput.Text;
            
            
            if (celsius != "")
                MessageBox.Show(converttofarenheit);
                //conversionlbl.Text = "celsius";
            else if (farenheit != "")
                MessageBox.Show(converttocelsius(convcelsius,2));
                //conversionlbl.Text = "converttofarenheit";
            else
                conversionlbl.Text = "Must enter number in only one box!";

        }


Im new to C# this is my first class..and i had trouble when i was first starting vb too...so im hoping that it will be something simple that im overlooking or forgetting about. Any help is appreciated..thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: calling a method

#2 Skaggles  Icon User is offline

  • THE PEN IS MIGHTIER
  • member icon





Reputation: 251
  • View blog
  • Posts: 640
  • Joined: 01-March 09

Re: calling a method

Posted 08 May 2010 - 10:37 AM

IF you'd like to turn the return value into a string to display with a message box, try adding .ToString() at the end of your call: converttocelsius(200.0).ToString()
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: calling a method

Posted 08 May 2010 - 12:28 PM

Just want to point out that your converttocelcius is wrong (it will return the wrong values)
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: calling a method

Posted 08 May 2010 - 01:44 PM

I dont think this is right
MessageBox.Show(converttocelsius(convcelsius,2));
your function, besides the said above, does not receives 2 parameters only one, and since it receive a double as value you should be sending option 1:
MessageBox.Show(converttocelsius(double.Parse(sfarenheit)).ToString());

option 2:
dfarenheit = double.Parse(sfarenheit);
MessageBox.Show(converttocelsius(dfarenheit).ToString());


Where sfarenheit is string, and dfarenheit is double.

This post has been edited by poncho4all: 08 May 2010 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: calling a method

Posted 08 May 2010 - 03:31 PM

First, it's true your formula is incorrect. It should look like this (for F to C)

double convcelsius = (farenheit - 32.0) / 1.8;



So your conversion method should look like this

private double converttocelsius(double farenheit)
{
    double convcelsius = (farenheit - 32.0) / 1.8;
    return convcelsius;
}



But as mentioned I would change it so it returns a string like this

private string converttocelsius(double farenheit)
{
    return string.Format("Converted: {0}", (farenheit - 32.0) / 1.8);
}



Then to use it (made some slight modifications to your code, hope you don't mind :) )

private void conversionbtn_Click(object sender, EventArgs e)
{
    double c, f;

    if (Double.TryParse(celsiusinput.Text, out c))
    {
        MessageBox.Show(converttofarenheit);
    }
    else if (Double.TryParse(farenheitinput.Text, out f))
    {
        MessageBox.Show(converttocelsius(f));
    }
    else
    {
        conversionlbl.Text = "Must enter number in only one box!";
    }
}



Hope that helps :)
Was This Post Helpful? 2
  • +
  • -

#6 Munkybones  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 31-October 09

Re: calling a method

Posted 10 May 2010 - 10:53 AM

K first i did fix the algorithm for the conversion methods. and have tried a couple of calls with message box
MessageBox.Show(converttocelsius(convcelsius).ToString);

i could not get this to work gives me an error about converting method to string.
but i was just using the message box's to try and help me see if any part of it is working. I actually need to figure out how to call to a label i just dont understand the syntax of how to word it so i can call the method i have set up with return value. Is there a special way that you are supposed to set up the return..or call the return to a label?
still have it to pass the value of input temperature to the method for the conversion then return convcelsius/convfarenheit but how do you call convcelsius or convfarenheit
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: calling a method

Posted 10 May 2010 - 11:13 AM

You obviously skipped completely over my post?
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: calling a method

Posted 10 May 2010 - 11:43 AM

ToString() is a method, therefore you need the opening and closing parenthesis after it.

MessageBox.Show(converttocelsius(convcelsius).ToString());


Was This Post Helpful? 0
  • +
  • -

#9 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • Posts: 162
  • Joined: 01-August 08

Re: calling a method

Posted 10 May 2010 - 11:49 AM

If you are trying to output the result to a label, you could try something like this:
conversionlbl.Text = converttocelsius(32);


As PsychoCoder had stated previously, you could apply formatting to your value when it is returned or apply the formatting when you set the text for your label.

conversionlbl.Text = string.Format("{0:0}", converttocelsius(32));

This post has been edited by sithius92: 10 May 2010 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#10 Munkybones  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 31-October 09

Re: calling a method

Posted 11 May 2010 - 09:54 AM

Psychocoder, no i didnt mean for it to seem like that. I had tried some stuff with how you had it built up but there were things that i didnt understand like the outc within the if statements. i figured that was something specific with messagebox's. but on that note thank you for showing me a better way of writing the methods that i had set up...cut's down only 1 line of code but good to know it can be set up inline setting variable and declaring it within the same line.

And on the issue of formatting the line
conversionlbl.Text = string.Format("{0:0}", converttocelsius(32));

i have learned only vb and am confused as to the way the last part of this strign is set up. I thought that anything that you put within "" will come out as text to wherever you have it set to display.
My other question in this line is the (32) what is this value for? my book is unclear my thinking is its the value your calling from that method. and if that is the case why 32 in this situation when the value will be changing on a regular?
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: calling a method

Posted 11 May 2010 - 10:22 AM

Answering your questions backwards:

The 32 is the parameter being sent to the method convertocelcius().

As for the "" part, String.Format() (in this case) has 2 parameters. The first is a string used to define how you want the output to look, the second is the data to be formated.
Was This Post Helpful? 0
  • +
  • -

#12 Munkybones  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 31-October 09

Re: calling a method

Posted 12 May 2010 - 02:05 PM

ok with the parameter that is what i had thought it was, now that is a set number ...32 is going to come through and display...but how would i got about using my inputtextbox as my variable?

Thanks for explaining the format set up too
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1