繁体   English   中英

C#类中的Switch语句

[英]Switch Statement in C# Class

我有一个带有xml配置文件的csharp应用程序,其中包含一个名为“环境”的元素,可以将其设置为不同的值(例如,开发/测试/生产)。

修改此配置文件条目后,应用程序中生成的全局变量将全部更改。 我的应用程序中有一个名为Globals的类,用于存储全局变量。 我想向其中添加一个case / switch元素,但是它似乎无法正常工作。

因此,例如,我在globals类的顶部定义了以下内容:

public static string environment = MyApp.Properties.Settings.Default.Environment;

然后在我的课堂上降低我要执行以下操作:

  switch (environment)
    {
        case "development":
            public static string Server = "SQL1";
            public static string Username = "dev.user";
        case "test":
            public static string Server = "SQL2";
            public static string Username = "test.user";
        case "production":
            public static string Server = "SQL3";
            public static string Username = "prod.user";
        default:
            public static string Server = "SQL1";
            public static string Username = "dev.user";
    }

(在上面的示例中,我将变量的数量减少到两个,只是为了使其更易于理解,但实际上每个环境中可能有30个变量。)

这不起作用,我得到多个错误:

类,结构或接口成员声明中的无效令牌“ switch”
类,结构或接口成员声明中的无效令牌“)”无效

还有其他方法吗?

谢谢布拉德

 public static class Globals 
 {
     public static string Environment = MyApp.Properties.Settings.Default.Environment;
     public static string Server; 

     // rest of code

     public static void LoadEnvironment() 
     {
        switch (Environment)
        {
            case "development": 
            {
                Server = "SQL1";
                Username = "dev.user";
                break;
            }

           // rest of code
        }
     }
}

基于该错误,编译器认为它是在类的主体内进行编码的。 尝试将逻辑移至某个方法或某些方法中,这可能是由于switch语句内的访问修饰符所致-例如。 公共静态等

  1. public static应该在类范围内声明,而不是在函数内声明。
  2. 您可以在声明行中或在静态构造函数中初始化静态变量。
  3. 您忘记了在每个案例末尾都放置“中断”。

因此,代码应如下所示:

public class MyClass

{

public static string Server;
public static string Username;

static MyClass()
{
    switch (environment)
    {
        case "development":
            Server = "SQL1";
            Username = "dev.user";
            break;

        case "test":
            Server = "SQL2";
            Username = "test.user";
            break;

        case "production":
            Server = "SQL3";
            Username = "prod.user";
            break;

        default:
            Server = "SQL1";
            Username = "dev.user";
            break;
    }

}

}

尝试在switch语句之前定义您的字符串。 对于每种情况,都必须定义一个break语句,以使指针脱离switch结构。

对于您的情况,最好将ServerUsername定义为属性,并在静态类的静态构造函数中,从对象中定义这些值。 样品:

public static class Globals
{
    // define properties
    public static string Server { get; set; }
    public static string Username { get; set; }

    // encapsulate the Settings.Environment in a property
    public public static string Environment 
    {
       get { return MyApp.Properties.Settings.Default.Environment; }
    }

    // when the application starts, this static scope will execute!
    static Globals()
    {
        switch (environment)
        {
            case "development":
                Server = "SQL1";
                Username = "dev.user";
                break;
            case "test":
                Server = "SQL2";
                Username = "test.user";
                break;
            case "production":
                Server = "SQL3";
                Username = "prod.user";
                break;
            default:
                Server = "SQL1";
                Username = "dev.user";
        }
    }
}

要使用它,只需致电

var server = Globals.Server;
var user = Globals.Username;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM