小编cyl*_*lee的帖子

C#中的switch-case和out参数

string returnString;
switch(Type) {
    case 0: returnString = exampleFunction(foo0, out int exitCode); break;
    case 1: returnString = exampleFunction(foo1, out int exitCode); break;
    case 2: returnString = exampleFunction(foo2, out int exitCode); break;
}
Run Code Online (Sandbox Code Playgroud)

当我编写这段代码时,VS向我显示了一个错误:'名为'exitCode'的局部变量已在此范围内为第4行和第5行定义.因此,如果我想获得两个以上不同类型的变量exampleFunction, 我该怎么办?如果我不能out在代码中使用,我应该使用元组来获取returnStringexitCode从函数中获取吗?

c# switch-statement

0
推荐指数
1
解决办法
743
查看次数

如何取消关闭winform?

关闭表单时,FormClosed会发生事件,我想在FormClosed事件发生时进行一些工作,如:

this.FormClosed += (s, e) => {
    var result = MessageBox.Show("Exit Program?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcons.Question);
    if (result == DialogResult.No) {
        return;
    } else {
        // Do some work such as closing connection with sqlite3 DB
        Application.Exit();
    }
};
Run Code Online (Sandbox Code Playgroud)

问题是无论我在消息框中选择是或否,程序都会关闭.如果我选择不,我需要中止程序退出,所以我该怎么做?

c# winforms

0
推荐指数
2
解决办法
557
查看次数

标签 统计

c# ×2

switch-statement ×1

winforms ×1