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

Related Questions

Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
Update: Edited code example to use AutoA for the workaround (which was the original
i am trying to compile example source code which is using the OpenGL, SDL
Example code: <html> <head> <script src=jquery-1.3.2.min.js type=text/javascript></script> <script src=jquery-ui-1.7.1.custom.min.js type=text/javascript></script> </head> <body> <table border=1
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
The example code below works as as a server process. But when I add
The example code of section 10.6, the expected result is: after several iterations, the
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
Just some example code here, but I have lists of strings that I want

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.