c# - Using 'switch' with strings in resource file -
i have bunch of strings in resource(.resx) file. trying directly use them part of switch statement (see sample code below).
class test { static void main(string[] args) { string case = args[1]; switch(case) { case stringresources.cfg_param1: // something1 break; case stringresources.cfg_param2: // something2 break; case stringresources.cfg_param3: // something3 break; default: break; } } }
i looked @ of solutions, of them seem suggest need declare them const string
dislike. liked top voted solution question: using collection of strings in switch statement. need make sure enum
, strings
in resource file tied together. know neat way of doing that.
edit: found this great answer while researching how use action
:
you use dictionary<string, action>
. put action
(a delegate method) each string in dictionary , search it.
var actions = new dictionary<string, action> { { "string1", () => method1() }, { "string2", () => method2() }, { "string3", () => method3() }, }; action action; if (actions.trygetvalue(mystring, out action)) { action(); } else { // no action found }
as sidenote, if method1
action
or void method1()
method (with no parameters , no return value),
{ "string1", (action)method1 },
Comments
Post a Comment