marquee
marquee

Reputation: 25

Show the variable in the Message Box from Method Class

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

Upvotes: 1

Views: 671

Answers (3)

tomRedox
tomRedox

Reputation: 30443

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.

Upvotes: 4

Ian
Ian

Reputation: 30813

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
    }
}

Upvotes: 3

Seth Kitchen
Seth Kitchen

Reputation: 1566

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

MessageBox.Show(ClassName.Hours());

Upvotes: 3

Related Questions