c# - How to convert string from a configuration file to a readable key? -


i have configuration .xml file contains following keys:

<add key="key1" value="d1"/> <add key="key2" value="d2"/> <add key="key3" value="d3"/> <add key="key4" value="d4"/> <add key="key5" value="d5"/> 

now need keys used here, tried doesn't work:

string k1 = system.configuration.configurationmanager.appsettings["key1"]; string k2 = system.configuration.configurationmanager.appsettings["key2"]; string k3 = system.configuration.configurationmanager.appsettings["key3"]; string k4 = system.configuration.configurationmanager.appsettings["key4"]; string k5 = system.configuration.configurationmanager.appsettings["key5"];  keys key1 = (keys)enum.parse(typeof(keys), k1); keys key2 = (keys)enum.parse(typeof(keys), k2); keys key3 = (keys)enum.parse(typeof(keys), k3); keys key4 = (keys)enum.parse(typeof(keys), k4); keys key5 = (keys)enum.parse(typeof(keys), k5);  switch (e.keycode)  {   case keys.key1:    //something happens here   break;   case keys.key2:    //something happens here   break;   case keys.key3:    //something happens here   break;   case keys.key4:    //something happens here   break;   case keys.key5:    //something happens here   break;  } 

this errors get:

error   1   'system.windows.forms.keys' not contain definition 'key1' error   2   'system.windows.forms.keys' not contain definition 'key2' error   3   'system.windows.forms.keys' not contain definition 'key3' error   4   'system.windows.forms.keys' not contain definition 'key4' error   5   'system.windows.forms.keys' not contain definition 'key5' 

what doing wrong?

you won't able use switch statement in case. there no way have case values who's value initialized @ runtime. switch case requires constant value , constants have compile time constant.

use if, else if etc. statement instead , compare string values config keycode.tostring() value.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -