c# - 在方法类的消息框中显示变量

标签 c# .net winforms

我有一个问题,当用户单击我的表单中的按钮时,我如何从我的方法类中显示变量小时,然后变量/术语将在消息框中显示它。

这是我的代码

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

相关文章:

c# - Signalr 2.2.0 私有(private)消息传递

c# - 在 C# 中调用 c++ dll 函数时发生访问冲突

.net - MemoryStream 上的 BinaryReader 保持为空

c# - 在 VB.Net 或 C# 中保存设置

c# - Visual Studio C# Win-Forms 应用程序网络浏览器加载速度非常慢

c# - 通过调用代表来引发事件 - 为什么不通过事件名称?

c# - 根据c#中的元素大小对数组进行排序并返回数组的索引

c# - F5 或开始调试按钮对于 Winform 应用程序是灰色的吗?

.net - ISV 公司的最佳 SAP 集成实践?

.net - 起订量:高级模拟设置