Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:37:05+00:00 2026-05-13T07:37:05+00:00

I want to call a method based on a value in a lookup table.

  • 0

I want to call a method based on a value in a lookup table. The values will be looked up in a database, and will be iterated through. What I am trying to avoid is this:

foreach (Row r in rows)
{
   if (r["command"] == "Command1")
         MyClass.Command1();
   else if (r["command"] == "Comman2")
         MyClass.Command2();
   else if (r["command"] == "Comman3")
         MyClass.Command3();
}

This is legacy code that I have to support, but I know there surely is a better way to do this. Currently the code looks like the above but I am looking for a more elegant solution.

EDIT:

Based on the suggestions below, I am trying to do something like this:

    static void Main(string[] args)
    {

        Dictionary<string, Action<MyClass>> myActions = new Dictionary<string,Action<MyClass>>();
        myActions.Add("Command1",MyClass.DoCommand1("message1"));
        myActions.Add("Command2",MyClass.DoCommand1("message2"));

        myActions["Command1"]();

    }

with my class file looking like this:

public class MyClass
{
    public void DoCommand1(string message)
    {
        Console.WriteLine(message);
    }

    public void DoCommand2(string message)
    {
        Console.WriteLine(message);
    }
}

However, I am getting syntax errors saying that an object reference is required for the nonstatic filed, method, or property MyClass.DoCommand1(string). Any ideas?

Please note I am using the .NET 2.0 framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:37:06+00:00Added an answer on May 13, 2026 at 7:37 am

    You can use reflection:

    string command = (string)r["command"];
    typeof(MyClass)
        .GetMethod(command, BindingFlags.Static | BindingFlags.Public)
        .Invoke (null, null);
    

    Or you can also use delegates:

    var actionMap = new Dictionary<string, Action<string>> {
        {"SomeAction", MyClass.SomeAction},
        {"SomeAction2", MyClass.SomeAction2},
        {"SomeAction3", MyClass.SomeAction3},
    };
    actionMap[r["command"]]("SomeString");
    

    With delegates you get a nice syntax and avoid the performance hit of reflection.

    UPDATE:
    I noticed that you’re using .NET 2.0, you need to do:

    class Program
    {
        delegate void PoorManAction (string param);
        static void Main(string[] args)
        {
    
            Dictionary<string, PoorManAction> actionMap = new Dictionary<string, PoorManAction>();
            actionMap.Add("SomeMethod1", MyClass.SomeMethod1);
            actionMap.Add("SomeMethod2", MyClass.SomeMethod2);
            actionMap.Add("SomeMethod3", MyClass.SomeMethod3);
            actionMap.Add("SomeMethod4", MyClass.SomeMethod4);
            actionMap[r["command"]]("SomeString");
    
        }
    }
    

    UPDATE 2:: Now the example uses methods with a string parameter as seen in the updated question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Don't use the ancient join syntax: FROM A, B, use… May 13, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer In your mapViewController create an action method: - (void)openAnnotation:(id)annotation {… May 13, 2026 at 8:12 pm
  • Editorial Team
    Editorial Team added an answer Solved it. I used: if (CreateProcess(TEXT("C:\\Program Files\\Java\\jre6\\bin\\java.exe"), TEXT(" -jar test.jar"),… May 13, 2026 at 8:12 pm

Related Questions

I have an application that I am writing that modifies data on a cached
By immutable function or immutable method, I mean a function whose result will never
I'm using CakePHP for a small web app and on one form page there's
I'm trying to come up with the best way to handle configuration in an
I have two action methods that are conflicting. Basically, I want to be able

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.