Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从下拉值初始化类变量_C#_Class_Case - Fatal编程技术网

C# 从下拉值初始化类变量

C# 从下拉值初始化类变量,c#,class,case,C#,Class,Case,我的winforms c应用程序上有两个窗体 第一个表单是一个登录表单,它对active directory执行身份验证,并执行一些授权检查,以允许用户继续。表单上还有一个下拉列表,其中包含以下选项: Environment: Development Staging Production 一旦用户被审查,在隐藏登录表单和显示主应用表单之前,我将设置以下内容: Globals.environment = ((string)this.cmboEnvironment.SelectedItem).T

我的winforms c应用程序上有两个窗体

第一个表单是一个登录表单,它对active directory执行身份验证,并执行一些授权检查,以允许用户继续。表单上还有一个下拉列表,其中包含以下选项:

Environment:

Development
Staging
Production
一旦用户被审查,在隐藏登录表单和显示主应用表单之前,我将设置以下内容:

Globals.environment = ((string)this.cmboEnvironment.SelectedItem).ToLower();
Globals.cs

public partial class Globals
{
    // A flag which denotes the environment that the tool should run against (staging/development/production)
    public static string environment;
    public static string server;
    static Globals()
    {
        switch (environment)
        {
            case "development":
                server = "Dev-Server";
                break;
            case "staging":
                server = "Staging-Server";
                break;
            case "production":
                server = "Production-Server";
                break;
            default:
                server = "Dev-Server";
                break;
        }
   }
}
我发现无论我选择哪个下拉列表,服务器值总是设置为Dev server

我认为现在发生的是,在调用设置环境值之前,正在实例化Globals对象,因此case语句默认为default:case

在填充所有其他全局值之前,我不知道如何设置环境。有人能帮我吗

谢谢 Brad

您的猜测是正确的。默认大小写一直在执行,这是因为,输入环境与给定大小写开关中的任何一个都不匹配。该开关对给定表达式和大小写执行区分大小写的比较。您需要更改与下拉列表中相同的大小写或用法。如下图所示:

switch (environment.ToLower())
{
        case "development":
            server = "Dev-Server";
            break;
        case "staging":
            server = "Staging-Server";
            break;
        case "production":
            server = "Production-Server";
            break;
        default:
            server = "Dev-Server";
            break;
}
您可以这样做:

public partial class Globals
{
    // A flag which denotes the environment that the tool should run against (staging/development/production)
    public static string environment;
    public static string server;


    public static void SetEnvironment(string env)
    {
        environment = env;
        switch (env)
        {
            case "development":
                server = "Dev-Server";
                break;
            case "staging":
                server = "Staging-Server";
                break;
            case "production":
                server = "Production-Server";
                break;
            default:
                server = "Dev-Server";
                break;
        }
    }
}
然后:

Globals.SetEnvironment(((string)this.cmboEnvironment.SelectedItem).ToLower());

静态构造函数在设置属性之前被调用。移除构造函数中的复选框。我在这里降低下拉值:Globals.environment=stringthis.cmboEnvironment.SelectedItem.ToLower;这很有效-谢谢你@不幸运的是,我认为它是有效的,因为在使用SetEnvironment方法设置环境之前,我们不会调用case语句。问题是,在运行main方法之前,静态构造函数是第一个被调用的对象,这就是为什么他必须将服务器初始化代码从静态构造函数移动到一个静态方法,该方法将初始化具有适当值的服务器字段。