c# - Using enum item to call a method -


i have enum 30 items in it. each item has corresponding function same name. able call function referencing enum @ position.

so if value @ enum[0] = foo, able call foo(string bar) using enum(0)("foobar")

in end point running each function task so:

enum test { aa, bb, cc, dd ....} tasks[0] = task.run(() => { prices[0] = aa("a string"); }); tasks[1] = task.run(() => { prices[1] = bb("a string"); }); tasks[2] = task.run(() => { prices[2] = cc("a string"); }); //for 30 tasks 

what along lines of:

enum test { aa, bb, cc, dd ....} (int = 0; < 30; i++) {     tasks[i] = task.run(() => { prices[i] = (test)i("a string"); }); } task.waitall(tasks.toarray()); 

is possible?

edit:

the enum relates controls on form have array of textboxs, label , array of prices populated results of functions:

    enum dealers { dealer1, dealer2 ... dealer29, dealer30 };      static int noofdealers = enum.getnames(typeof(dealers)).length;     decimal[] prices = new decimal[noofdealers];     textbox[] textbox = new textbox[noofdealers];     label[] boxes = new label[noofdealers];      (int = 0; < noofdealers; i++)     {         textbox[i] = controls.find("txt" + (dealers)i, true)[0] textbox;         boxes[i] = controls.find("box" + (dealers)i, true)[0] label;         prices[i] = 0;     }      //run 30 tasks populate prices array      (int = 0; < noofdealers; i++)     {        textbox[i].text = "£" + prices[i].tostring();     }      //loop through prices array , find cheapest price, colour label background green text box name @ enum value whatever 

i guess trying make code concise possible, there potential amount of tasks double , didn't want end 60 lines populate tasks array

i create dictionary , map enum actions:

  dictionary<test, func<string,double>> actions = new dictionary<test, func<string,double>>()             {                 {test.aa, (x) => { return 5;}},                 {test.bb, (x) => { return 15; }},             }; //x string              var res = actions[test.aa]("hello"); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -