Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to solve Index was outside the bounds of the array in c# language.

this is my code

C#
public void ProductCode()
        {
            ulong urunkodu = 200000000000;
            int i;
            int counter = 0;
            int productcodefilecounter =0;
            string line;
            ulong barkodnumarasi;

            try
            {
                System.IO.StreamReader file =   new System.IO.StreamReader("malincinsi.txt");

                while ((line = file.ReadLine()) != null)
                {
                    counter++;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Malın Cinsi File could not be accessed. " + ex.Message);
            }
                 
            string[] ListLines = new string[counter];

            counter++;

            try
            {
                System.IO.StreamReader productcodefile = new System.IO.StreamReader("productcode.txt");

                while ((line = productcodefile.ReadLine()) != null)
                {
                    productcodefilecounter++;
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Product Code File could not be accessed. " + ex.Message);
            }

            string[] ListProductCodeFileLines = new string[productcodefilecounter];

            try
            {
                for (i = 0; i < counter; i++)
                {
                    if (comboBox1.Text == ListLines[i])
                    {
                        urunkodu = Convert.ToUInt32(ListProductCodeFileLines[i]);
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("For iteration was unable to enter " + ex.Message);
                //Here ex.Message says Index was outside the bounds of the array.
            }

            double g = Convert.ToDouble(textBox5.Text);
            g = g * 1000;
            ulong ig = Convert.ToUInt32(g);
            barkodnumarasi = urunkodu + ig;
            textBox6.Text = Convert.ToString(barkodnumarasi);
        }
Posted
Comments
[no name] 24-Jul-14 18:04pm    
There is really nothing to "solve". Makes sure that your index is within the bounds of the array that you are accessing.
PIEBALDconsult 24-Jul-14 18:09pm    
Try stepping through it with the debugger.

1 solution

Hello,

Arrays in c# are 0 based that means that the first entry has an index of 0 and the last entry has an index of n-1.

There is a mistake in your code, you start by counting the number of lines and then create an array that size.
The issue is that you increment the counter by 1 just after creating the array.

C#
string[] ListLines = new string[counter];
counter++;


So what happens next when you iterate the array is that you try to access the element "n" but the array stops at "n-1"

C#
for (i = 0; i < counter; i++)
                {
                    if (comboBox1.Text == ListLines[i])
                    {
                        urunkodu = Convert.ToUInt32(ListProductCodeFileLines[i]);
                    }
                }


Basically you are reading an element that does not exist. This throws an exception index out of bounds because "n" is bigger that "n-1"

To fix this just remove the useless
C#
counter++; 
and your code will work fine.

Valery.
 
Share this answer
 
v2
Comments
alidayan 25-Jul-14 6:29am    
Thank you for your attention. now I have another problem.

for (i = 0; i < counter; i++)
{
if (comboBox1.Text == ListLines[i])
{
urunkodu = Convert.ToUInt32(ListProductCodeFileLines[i]);
}
}

is not working but there is no ex.Message it could not find the product code from productcode.txt
Valery Possoz 25-Jul-14 8:46am    
Where do you assign data to comboBox1? and what is the content of your text file?
Put a break point on "if (comboBox1.Text == ListLines[i])" press F9 in Visual Studio to set a break point.
Look at the value of your variables and see why if does not work.
String comparison is case sensitive, try "comboBox1.Text.ToLower() == ListLines[i].ToLower()"
Also spaces can be a problem... try "comboBox1.Text.Trim().ToLower() == ListLines[i].Trim().ToLower()"
alidayan 25-Jul-14 10:10am    
malincinsi.txt includes
1. Product
2. Product
3. Product
4. Product
5. Product

productcode.txt includes
201000100000
201000200000
201000300000
201000400000
201000500000

I have set lots of break point and I've seen that ListLines[i] is null everytime. But the counter is true I have seen it as 5 and productcodefilecounter is 5 also.
Valery Possoz 25-Jul-14 10:51am    
Yes it will be null... you've just declared the array. you need to fill it with data.
string[] lines = File.ReadLines("malincinsi.txt").ToArray();
string[] ListProductCodeFileLines = File.ReadLines("productcode.txt").ToArray();
alidayan 25-Jul-14 11:09am    
Thank you but not File.ReadLines, it must be File.ReadAllLine I am really greatfull.

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