Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This line throws IndexOutOfRangeException(index was outside the bounds of array) and it is pointed on this line in the code:
C#
names[i] += txtName.Text + "\r\n";


This happens only if I stop entering names(lets say after entering few of them and after hitting show names button, I get the list that I entered. But when I continue entering name again, I get the exception error?

So if I continue entering names upto 100 without hitting show names in the middle, no error. I should be able to contunie add names after hitting the show names as long as it is not beyond 100 names.

Any ideas why?



<pre lang="C#">namespace Arrays
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }



        //initialize the Array
        string[] names = new string[100];

        int i = 0;


        //Enter  Names up to 100 and store them in array
        private void btnEnterName_Click(object sender, EventArgs e)
        {

            names[i] += txtName.Text + "\r\n";

            txtName.Clear();

        }



        //Display stored Names in Array using foreach loop in multiline textbox
        private void btnShowNames_Click(object sender, EventArgs e)
        {
            foreach (var item in names)
            {

                txtNames.Text += names[i] + "\r\n";

                i++;


            }

        }

    }
}




What I have tried:

tried i++ after the line where error is pointed but no success.
Posted
Updated 26-Feb-16 18:14pm

1 solution

You're having problems with the variable i: first you're storing every txtName.Text in the same array-item (names[0] - that's not what you want but it's not the source of the error) and then you're increasing i (in btnshowNames) beyond the size of the array (= 100) before continuing with btnEntername where i is then too large (100) and you get the error. So, don't use a variable for more than one purpose at a time.

Let's rename i to index and your code should be:

private void btnEnterName_Click(object sender, EventArgs e)
{
    if (index < names.Length)
    {
        names[index++] = txtName.Text;
        txtName.Clear();
    }
    else
    {
        // array 'full'
    }
}
 
private void btnShowNames_Click(object sender, EventArgs e)
{
    txtName.Clear();
    foreach (string item in names)
    {
        txtNames.AppendText(item + Environment.NewLine);
    }
}


Peter
 
Share this answer
 
Comments
Member 11829482 27-Feb-16 20:53pm    
Thanks. works good. One thing noticed that after clicking show names, it puts spaces between entries. why is it?
Member 11829482 27-Feb-16 21:16pm    
also it duplicates the entries create line breaks after entries. Scroll bar of the textbox jump down after hitting show Names button.
[no name] 27-Feb-16 23:03pm    
I don't know why it 'puts spaces between entries' and 'duplicates entries': please compare your code to my example carefully, it should not do that.
When you add text to the textbox it scrolls down, you can't prevent that. You can scroll (back) to the first line with: txtName.SelectionStart = 0; txtName.ScrollToCaret();
Also if you don't want to add empty array items (strings), use: if (string.IsNullOrEmpty(item)).
For a bit faster and 'smoother' adding of the items to the textbox use txtName.SuspendLayout() and txtName.ResumeLayout().
Member 11829482 28-Feb-16 0:14am    
I figured it out. Thanks.
Member 11829482 28-Feb-16 0:15am    
Now that I want the code where it is.

How can I try/catch error check to the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900