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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:17:10+00:00 2026-05-25T00:17:10+00:00

I am writing a simple foreach loop to read key values from appconfig file.

  • 0

I am writing a simple foreach loop to read key values from appconfig file. For some reason the array string called server looses previous values each time the foreach loop increments. Once the program runs to completion I see values as (server[0] = null, server[1]=null, server[2] = ). Only the last array pointer server[3] retains the values.

My questions are:

  1. Why does the server[0] and server[1] become null.
  2. How can I retain all the array values and pass them outside the foreach loop?

    private void button2_Click(object sender, EventArgs e)
    {
        int i = 0;
        int max = 0;
        string[] server;
    
        foreach (string key in System.Configuration.ConfigurationManager.AppSettings)
        {
            max = max + 1;
        }
    
        foreach (string key in System.Configuration.ConfigurationManager.AppSettings)
        {
            server = new string[max];
            server[i] = key;
            MessageBox.Show(server[i]).ToString();
            i = i + 1;
        }
    }
    
  • 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-25T00:17:11+00:00Added an answer on May 25, 2026 at 12:17 am
    server = new string[max];
    

    Should be outside the loop (in your case, between the two), otherwise you create a new array every iteration. This explains why the last value is correct, and the rest are empty.

    Also, you can use ++ instead of i + 1 and max + 1.

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

Sidebar

Related Questions

a quick, simple question from me about for-loops. Situation I'm currently writing some high-performance
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I'm currently writing a simple .sh script to parse an Exim log file for
I was writing a simple PHP page and a few foreach loops were used.
I am developing a simple WinAPI application and started from writing my own assertion
I'm in the process of writing a simple prototype in Node.js with some helper
I am writing a simple email helper class that will be called by a
I am writing a simple XML file parser using LINQ to XML. I want
I'm writing a simple QR code generator (just for fun and to learn some
I am writing a simple parser that will take a string of the format

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.