-1

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

Brad
  • 1,979
  • 7
  • 35
  • 47
  • 2
    remove all public static string in case statement, declare top of class – Luke Hutton Mar 24 '16 at 23:48
  • Could you not use standard config transforms instead? Web.Debug.Config, Web.Test.Config etc http://stackoverflow.com/questions/15683854/how-do-i-change-an-web-config-setting-using-transformation-syntax this would also give you the ability to change values at runtime. – Macilquham Mar 24 '16 at 23:51
  • The variables defined should not be user changeable - we want them hard coded into the app. The only thing the user should be able to do is change the environment from the config file. – Brad Mar 24 '16 at 23:54
  • did you run with break points? Are you seeing the expected value in 'environment'? adding break statements in each case wouldn't hurt either. Like Luke said you probably want to run the switch from within a private method. User would not be able to change – DaniDev Mar 24 '16 at 23:58

4 Answers4

2
 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
        }
     }
}
Luke Hutton
  • 10,612
  • 6
  • 33
  • 58
1

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

Jaya
  • 3,721
  • 4
  • 32
  • 48
1
  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;
    }

}

}

Ilan
  • 624
  • 6
  • 11
0

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;
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194