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 4017458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:51:48+00:00 2026-05-20T09:51:48+00:00

I wanted to do something ‘dynamic’ with a dictionary object and supplied command line

  • 0

I wanted to do something ‘dynamic’ with a dictionary object and supplied command line arguments. The command line arguments are Boolean and I could then call methods if any one of them was true. So…

public class CommandLineArguments
{
    public bool AddSection1 { get; set; }
    public bool AddSection2 { get; set; }
    public bool Addsection3 { get; set; }        
}

class RunSomeActions
{
    private Dictionary<bool, Action> methodList = new Dictionary<bool, Action>();

    public RunSomeActions()
    {
        // create the switches as if from a command line
        CommandLineArguments parameters = new CommandLineArguments();
        parameters.AddSection1 = true;
        parameters.AddSection2 = false;
        parameters.Addsection3 = true;

        // setup the methods for the switches
        methodList.Add(parameters.AddSection1, this.Section1);
        methodList.Add(parameters.AddSection2, this.Section2);
        methodList.Add(parameters.Addsection3, this.Section3);

        foreach (var entry in methodList)
        {
            // if the switch is on
            // call the method
            if (entry.Key)
                methodList[entry.Key]();
        }


    }

    private void Section1()
    { 
        // add specific entries into a file
    }

    private void Section2()
    { 
        // perform analysis on a file
    }

    private void Section3()
    { 
        // delete everything and start again
    }

}

This works great if you only ever have two values of true and false, so it’s not much good really. What I did like about this approach was not having to parse the arguments manually and then build an Action list. Is there a way I can salvage this design?

  • 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-20T09:51:49+00:00Added an answer on May 20, 2026 at 9:51 am

    Since you’re not actually using the dictionary for lookups, but just for storage and iteration, instead of using a Dictionary<K,V>, you can just use a List<KeyValuePair<K,V>>.

    The main difference from a code perspective would be changing .Add to:

    methodList.Add(new KeyValuePair<bool, Action>(parameters.AddSection1, this.Section1));
    

    Then, when you use, switch to:

    foreach (var entry in methodList)
    {
        // if the switch is on
        // call the method
        if (entry.Key)
            entry.Value(); // Execute "value" directly
    }
    

    That being said, you can take this one step further, potentially, and just store a List<Action> directly. Only add actions to the list where the condition is true, then execute them all.

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

Sidebar

Related Questions

After some time I wanted to update my git repo, and then something went
I wanted to do something like this: <asp:Label ID=lblMyLabel onclick=lblMyLabel_Click runat=server>My Label</asp:Label> I know
I wanted to write something basic in assembly under Windows. I'm using NASM, but
Basically I wanted to do something like git push mybranch to repo1, repo2, repo3
If I wanted to do something like this: collection.each do |i| return nil if
I have been bitten by something unexpected recently. I wanted to make something like
This is something that's I've wanted to know recently, mostly out of curiousity. I'm
If I wanted to represent states or options or something similar using binary flags
I wanted to make Map of Collections in Java, so I can make something
I just caught myself doing something I do a lot, and wanted to generalize

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.