1

I having a problem on how can I show the variable hours from my method class when the user click the button in my form then the variable/term will show it the message box.

Here's my code

// Form1. The window form
private void button1_Click(object sender, EventArgs e)
{
    ClassName.Hours();
}

// My ClassName with method Hours()
public static void Hours() {
    var citiesDistance = new Dictionary<string, int> { 
        {"Place1",10},
        {"Place2",20},
        {"Place3",30},
    };

    var cities = "Place1";
    var length = citiesDistance[cities];

    var speed = 100;

    var hours = length / speed;

    return;
}
Ian
  • 30,182
  • 19
  • 69
  • 107
marquee
  • 25
  • 4

3 Answers3

4

Is this what you mean?

Form code:

// Form1. The window form
private void button2_Click(object sender, EventArgs e)
{
    MessageBox.Show(ClassName.Hours("Place1").ToString());
}

Class code:

    // My ClassName with method Hours()
    public class ClassName
    {

        // My ClassName with method Hours()
    public static decimal Hours(string place)
    {
        var citiesDistance = new Dictionary<string, int> 
        { 
            {"Place1",10},
            {"Place2",20},
            {"Place3",30},
        };


        var length = citiesDistance[place];

        decimal speed = 100;

        decimal hours = length / speed;

        return hours;

    }

You could change those decimals to doubles if you prefer too. This is a good discussion of which to use when.

Community
  • 1
  • 1
tomRedox
  • 28,092
  • 24
  • 117
  • 154
  • no sir... The method hours is located at my ClassName class. so its seperated file with file name extension of .cs – marquee Jan 14 '16 at 14:26
  • @marquee you could change those decimals to doubles if you prefer too. This is a discussion of which to use when: http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when – tomRedox Jan 14 '16 at 14:37
  • Ah thank you sir. what if I have if else? where do I have to put it? I have a combobox where if the user choose the Place2, or Place3, it will show change the var cities = "Place1"; to cities = "Place2" or cities = "Place3"? – marquee Jan 14 '16 at 14:38
  • @marquee added the place - any more questions probably need to go in a new question - or you need to edit your original question – tomRedox Jan 14 '16 at 14:43
3

Make Hours() return a string or int. Then in your button code, do

MessageBox.Show(ClassName.Hours());
Seth Kitchen
  • 1,526
  • 19
  • 53
  • Argument 1: cannot convert from void to 'string', Invalid expression term 'string', and this one, The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments. :_( help sir. – marquee Jan 14 '16 at 14:29
  • `MessageBox.Show(ClassName.Hours().ToString());` And the signature of Hours must be `public static decimal Hours()` – jpedro20 Jan 14 '16 at 14:30
  • @marquee the error is because you did not change your method to return something else, you can either return string or decimal/float/double and then have to string – John Demetriou Jan 14 '16 at 14:47
  • Thank you sir. I am done now. :) Thank you so much for the idea and new learning. have a good day – marquee Jan 14 '16 at 14:50
3

Your Hours method does not return anything. It must return something and since it can be a fraction, I suggest to return double

// My ClassName with method Hours()
public static double Hours() { //return double here
    var citiesDistance = new Dictionary<string, int> { 
        {"Place1",10},
        {"Place2",20},
        {"Place3",30},
    };

    var cities = "Place1";
    double length = citiesDistance[cities]; //use double

    double speed = 100; //use double

    double hours = length / speed; //use double

    return hours; //note that it is returned
}

And then in your main form

// Form1. The window form
private void button1_Click(object sender, EventArgs e)
{
    double hours = ClassName.Hours();
    //Do something with hours, example:
    MessageBox.Show(hours.ToString("f3")); //"f3" is to limit the number of fraction digits (this case being 3) it prints. Change 3 to any number you want
}

You can get what you want. To convert it to string, simply do hours.ToString()

Edit:

If you have user input (which is a comboBox), you should do it like this

// My ClassName with method Hours()
public static double Hours(string cities) { //return double here, note the user input
    var citiesDistance = new Dictionary<string, int> { 
        {"Place1",10},
        {"Place2",20},
        {"Place3",30},
    };

    //var cities = "Place1"; //Note that this is commented now
    double length = citiesDistance[cities]; //use double

    double speed = 100; //use double

    double hours = length / speed; //use double

    return hours; //note that it is returned
}

And when you call it, you call it like this

// Form1. The window form
private void button1_Click(object sender, EventArgs e)
{
    if (comboBox.SelectedIndex >= 0){ //to prevent non-selection to give crash
        double hours = ClassName.Hours(comboBox.SelectedItem.ToString());
        //Do something with hours, example:
        MessageBox.Show(hours.ToString("f3")); //"f3" is to limit the number of fraction digits (this case being 3) it prints. Change 3 to any number you want
    }
}
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Ah thank you sir. what if I have if else? where do I have to put it? I have a combobox where if the user choose the Place2, or Place3, it will show change the var cities = "Place1"; to cities = "Place2" or cities = "Place3"? – marquee Jan 14 '16 at 14:38
  • 1
    @marquee in that case, you need the Hours to have user input. Hours(string input) – Ian Jan 14 '16 at 14:39
  • I added it and I got this error sir, "No overload for method 'Hours' takes 0 arguments. :( – marquee Jan 14 '16 at 14:42
  • @tom_redox. hey thanks. you too. :D see the number of upvotes you have :D – Ian Jan 14 '16 at 14:49