在Method Class的Message Box中显示变量

时间:2016-01-14 14:18:47

标签: c# .net winforms

我遇到问题如何在用户单击表单中的按钮时显示方法类的变量小时数,然后变量/ term将显示​​消息框。

这是我的代码

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

3 个答案:

答案 0 :(得分:4)

这是什么意思?

表格代码:

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

班级代码:

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

    }

如果您愿意,也可以将这些小数更改为双倍。 T his is a good discussion时使用。

答案 1 :(得分:3)

Make Hours()返回一个字符串或int。然后在您的按钮代码中,执行

MessageBox.Show(ClassName.Hours());

答案 2 :(得分:3)

您的Hours方法不会返回任何内容。它必须返回一些内容,因为它可以是一小部分,我建议返回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
}

然后以你的主要形式

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

你可以得到你想要的东西。要将其转换为字符串,只需执行hours.ToString()

即可

编辑:

如果你有用户输入(这是一个comboBox),你应该这样做

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

当你打电话给它时,你可以这样称呼它

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