Index out of bounds exception in c#
        Posted  
        
            by 
                Naga
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Naga
        
        
        
        Published on 2012-10-25T22:31:11Z
        Indexed on 
            2012/10/25
            23:01 UTC
        
        
        Read the original article
        Hit count: 266
        
c#
I have the following code in my program which is throwing index out of bound exception at line yearList.SetValue(years[count], count);
protected void invoiceYear_DataBound(object sender, EventArgs e)
        {
           //invoiceYear.SelectedItem.Value= GetYearRange();
            String[] years = GetYearRange().Split(new char[] { '[', ',', ']',' ' });
            ListItem [] yearList = new ListItem[]{};
            System.Diagnostics.Debug.WriteLine("years-->" + years.Length);
            for (int i = 0; i < years.Length; i++)
            {
                System.Diagnostics.Debug.WriteLine("years-->" + years.GetValue(i));
            }
            int count = 0;
            foreach (String str in years)
            {
                if (string.IsNullOrEmpty(str))
                    System.Diagnostics.Debug.WriteLine("empty");
                else
                {
                    yearList.SetValue(years[count], count);
                    count++;
                }
            }
            //System.Diagnostics.Debug.WriteLine("yearList-->" + yearList.GetValue(0));
            //invoiceYear.Items.AddRange(yearList);
        }
© Stack Overflow or respective owner