简体   繁体   中英

Switch Statement in C# Class

I have a csharp app with a xml config file containing an element called "environment" which can be set to different values (development/test/production for example).

When this config file entry is modified the resulting global variables within the application should all change. I have a class in my application called Globals were I am storing global variables. I want to add a case/switch element to it but it won't seem to work.

So for example I have the following defined at the top of the globals class:

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

Then lower down in my class I'm trying to do the following:

  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";
    }

(In the example above I reduced the number of variables to two just to make it more understandable but in reality there are probably 30 variables per environment.)

This doesn't work I get multiple errors:

Invalid token 'switch' in class, struct, or interface member declaration
Invalid token ')' in class, struct, or interface member declaration Invalid token 'case' in class, struct, or interface member declaration

Is there some other way of doing this?

Thanks Brad

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

Based on the error the compiler thinks it is coded inside the body of the class. Try moving the logic inside a method or some such and this may be due to your access modifiers inside your switch statement - eg. public static etc

  1. public static should be declare in the class scope, not within a function.
  2. You can initializes static variable either in the line of declaration or within the static constructor.
  3. You forgot puting "break at the end of each case.

So the code should looke like this:

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;
    }

}

}

Try to define your strings before the switch statement. For each case, you have to define a break statement to make the pointer get out of the switch structure.

For your case, it's a good idea to define the Server and Username as properties and in a static constructor of a static class, define these values from the object. For sample:

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";
        }
    }
}

To use it, just call

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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