我有一个问题,当用户单击我的表单中的按钮时,我如何从我的方法类中显示变量小时,然后变量/术语将在消息框中显示它。
这是我的代码
// 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;
}
最佳答案
你是这个意思吗?
表单代码:
// 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;
}
如果您愿意,也可以将这些小数点更改为 double 。电话 his is a good discussion其中何时使用。
关于c# - 在方法类的消息框中显示变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791826/