6

How can I do this:

private const string myConstant = ConfigurationManager.AppSettings["key"];

I've got this error message: The expression being assigned to 'XXX' must be constant.

I know that this AppSettings will probably never change but I don't want to hard-coded the value in my controler as below:

private const string myConstant = "XXX";

I was thinking to create a Singleton but I don't know if this is really a good idea.. any suggestions?

Thank you :)

Tyler Durden
  • 347
  • 1
  • 3
  • 15
  • 3
    A constant requires the compiler to determine the value rather than at run time. Since AppSettings can change the compiler does not know what the actual value will be. So the code has to be in a module. So simply change line to private string myConstant = ""; Then in a module do myConstant = ConfigurationManage.AppSettings["key"]; – jdweng Jul 19 '17 at 04:27
  • 2
    You can also consider "read-only" - https://stackoverflow.com/questions/55984/what-is-the-difference-between-const-and-readonly – Subbu Jul 19 '17 at 04:29

0 Answers0