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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:32:27+00:00 2026-05-16T12:32:27+00:00

Example code : EDITED : clarified example. Sorry for any confusion. using System.Collections.Specialized; using

  • 0

Example code :

EDITED : clarified example. Sorry for any confusion.

        using System.Collections.Specialized;
        using System.Configuration;

    ...

        // get collection 1

        NameValueCollection c1 = ConfigurationManager.AppSettings;

        // get collection 2

        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = "c:\\SomeConfigFile.config";
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        KeyValueConfigurationCollection c2 = config.AppSettings.Settings;

        // do something with collections

        DoSomethingWithCollection(c1);
        DoSomethingWithCollection(c2);
...

    private void DoSomethingWithCollection(KeyValueConfigurationCollection c)
    {
        foreach(KeyValueConfigurationElement el in c)
        {
            string key = el.Key;
            string val = el.Value;
            // do something with key and value
        }
    }

    private void DoSomethingWithCollection(NameValueCollection c)
    {
        for(int i=0; i < c.Count; i++)
        {
            string key = c.GetKey(i);
            string val = c.Get(i);
            // do something with key and value
        }
    }

There are currently two versions of DoSomethingWithCollection to take either a NameValueCollection or KeyValueConfigurationCollection.

Is there a cleaner way of doing this, so that there is just one version of DoSomethingWithCollection ?

Thanks.

  • 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-16T12:32:28+00:00Added an answer on May 16, 2026 at 12:32 pm

    Well, DoSomethingWithCollection should accept two parameters – 1) ICollection/IEnumerable that will allow you to enumerate through all values 2) Delegate that will take the object and give you key and value back. So you need to essentially write two different methods to parse the item for both type of collections.

    Edit: A sample code to give an idea.

    delegate void KeyValueItemParser(object item, out string key, out string value);
    
    void DoSomethingWithCollection(ICollection items, KeyValueItemParser parser)
    {
    
       string key, value
       foreach(object item in items)
       {
          parser(item, out key, out value);
          // do whatever you want to with key & value
       }
    }
    

    Edit 2: Not sure what you mean by not needing a delegate – if you need to have one version of DoSomethingWithCollection, you need to have at least some code that work different across both collection. Delegate would be the simplest way. Another way would be define an interface that will provide a collection/enumeration of NamedValuePair/KeyValuePair and then write to wrapper classes implementing this interface for different kind of target collections. Template code would be

    interface IKeyValuePairCollection
    {
      int Count {get; }
      KeyValuePair<string, string> Item(int index) { get; }
    }
    
    class NamedValueCollectionWrapper : IKeyValuePairCollection
    {
    
       private NamedValueCollection _inner;
    
       public NamedValueCollectionWrapper(NamedValueCollection target) 
       {
         -inner = target;
       }
    
       // roll out ur implementation to IKeyValuePairCollection
    }
    
    class KeyValueConfigurationCollectionWrapper : IKeyValuePairCollection
    {
      ...
    }
    

    IMO, a too much work for a simple requirement.

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

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • 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 If you need to move data inside your server side,… May 16, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer You could use something like the Celery distributed task queue… May 16, 2026 at 2:37 pm
  • Editorial Team
    Editorial Team added an answer Here are a couple of possibilities depending on what you… May 16, 2026 at 2:37 pm

Trending Tags

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

Top Members

Related Questions

Update: Edited code example to use AutoA for the workaround (which was the original
Example code: http://jsbin.com/eqile3/ This is a slider with 2 handles, 1 for min and
Example code: <div class=ex>Bla bla bla bla</div> In the top will be background-image and
I'm currently writing a project report, and to explain the slightly crazy un-intuitive code
I have an asp:button control and am using the CommandName and CommandArgument parameters in
EDITED BELOW FOR UPDATES!!! Thank you for the help, please by all means point
I have a WinForm and few properties that are set on it. for example
I have a Drupal view which should output a video player using flash. I
Is there a utility similar to OllyDbg / SoftICE for java? I.e. execute class
I'm wondering if you can overload an operator and use it without changing the

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.