How to use an array of strings to handle the cases in a switch statement in C#? -


i have array

    public static string[] commands =     {            "command1",         "command2",         "command3",         "command4",         "command5",         "command6",         "command7"     }; 

i want use array in function

    public static bool startcommand (string commandname) {         //stuff         if (commandname == commands[0]) {             //stuff             return true;         }         else {             //stuff             switch (commandname) {                 case commands [1]:                     //stuff                     break;                 case commands [2]:                     //stuff                     break;                 case commands [3]:                     //stuff                     break;                 case commands [4]:                     //stuff                     break;                 case commands [5]:                     //stuff                     break;                 case commands [6]:                     //stuff                     break;                 default:                     return false;             }             //do stuff             return true;         }     } 

the error giving me "a constant value expected" each of cases.

i use if , else statements, think switch statement looks better this.

unless i've missed mark, array of constant strings, should work. appreciated. sorry if newb question, i've been programming c# 4 days.

what you're looking dictionary<tkey, tvalue> type. dictionary collection of key-value pairs, can take advantage of you're attempting achieve.

using example you've given, implementation this:

dictionary<string, action> commandsdictionary = new dictionary<string, action>(); commandsdictionary.add("command1", () => console.writeline("command 1 invoked")); commandsdictionary.add("command2", () => console.writeline("command 2 invoked"));  commandsdictionary["command2"].invoke(); // command 2 invoked 

as you'll have noticed, i've introduced action delegate without parameters.


to introduce parameter, specify type argument, this: action<int>

dictionary<string, action<int>> commandsdictionary = new dictionary<string, action<int>>(); commandsdictionary.add("command1", (i) => console.writeline("command {0} invoked", i));  commandsdictionary["command1"].invoke(1); // command 1 invoked 

if want return value delegate you're invoking, use func delegate, easy remember rule func last type parameter type being returned, func<int, string> equivalent method following signature public string foo(int i)

dictionary<string, func<int, string>> commandsdictionary = new dictionary<string, func<int, string>>(); commandsdictionary.add("command1", (i) => { return string.format("let's funky {0}", i); });  string result = commandsdictionary["command1"].invoke(56963); console.writeline (result); // let's funky 56963 



reference

i've added section aid not yet know delegate is... it's rather simple.


delegates

a delegate type represents references methods. they're variables declare reference objects, except instead of objects, reference methods.

a delegate can instantiated named method or anonymous function such lambda expression (which type i've demonstrated above).


the action delegate

the action delegate has return type of void , defines signature type parameters.

void example() {     // named method     this.namedactiondelegate = namedmethod;     this.namedactiondelegate.invoke("hi", 5);     // output > named said: hi 5      // anonymous function > lambda     this.anonymousactiondelegate.invoke("foooo", 106);     // output > anonymous said: foooo 106 }  public action<string, int> namedactiondelegate { get; set; } public action<string, int> anonymousactiondelegate = (text, digit) => console.writeline ("anonymous said: {0} {1}", text, digit);  public void namedmethod(string text, int digit) {     console.writeline ("named said: {0} {1}", text, digit); } 

the func delegate

the func delegate similar action delegate difference being func never returns void , require @ least 1 type argument , mentioned earlier, type argument specified last dictates return type of delegate.

void example() {     // named method     this.namedfuncdelegate = namedmethod;     string namedresult = this.namedfuncdelegate.invoke(5);     console.writeline (namedresult);     // output > named said: 5      // anonymous function > lambda     string anonyresult = this.anonymousfuncdelegate.invoke(106);     console.writeline (anonyresult);     // output > anonymous said: 106 }  public func<int, string> namedfuncdelegate { get; set; } public func<int, string> anonymousfuncdelegate = (digit) => { return string.format("anonymous said: {0}", digit); };  public string namedmethod(int digit) {     return string.format ("named said: {0}", digit); } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -