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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:46:41+00:00 2026-06-13T10:46:41+00:00

I have the following code in my program which is throwing index out of

  • 0

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);
        }
  • 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-06-13T10:46:42+00:00Added an answer on June 13, 2026 at 10:46 am

    You haven’t asked a question, so I’m going to guess your question is simply “Why?”

    yearList is defined as an empty array:

    ListItem [] yearList = new ListItem[]{};
    

    It’s length is always zero. Therefore you cannot set any elements of it, as it has no elements to set.

    UPDATE

    You’ve now asked: “But I am not getting how to declare a dynamic array??”

    There are no dynamic arrays in .NET. You have a number of different collection types depending on your scenario. I’ll suggest that List<ListItem> is probably what you want.

    List<ListItem> yearList = new List<ListItem>(); // An empty list
    

    Then

    yearList.Add(years[count]); // Adds an element to the end of the list.
    

    Alternatively, that whole loop could be better written as:

            foreach (String str in years)
            {
                if (string.IsNullOrEmpty(str))
                    System.Diagnostics.Debug.WriteLine("empty");
                else
                {
                    yearList.Add(str);
                }
            }
    

    Then you are not having to worry about the count and nor are you getting out of step (because you are only incrementing count when str contains something – which is probably not what you want)

    UPDATE 2
    And if you desperately do need an array at the end, you can always covert the list to an array using yearList.ToArray(), but remember to add using System.Linq; at the top of your file as it is an extension method that LINQ provides and not part of the List class itself.

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

Sidebar

Related Questions

I have the following line of code in my program which I took from
I have the following code which is executed from the command line: import cgi,time,os,json,sys,zipfile,urllib2
I currently have the following code. I get ArrayIndexOutofBoundsException at this line. Exception in
I have a Delphi program which contains the following code: procedure TForm1.Shape1MouseDown(Sender: TObject; Button:
I have the following code which should put programs startable in Bash. if [
I have the following code: class Program { static void Main(string[] args) { new
Assume that we have the following code: class Program { static volatile bool flag1;
i have written the following code class Program { static void Main(string[] args) {
I have the following code in my program: Thread getUsersist, getChatUsers; getUsersList = new
I have the following code in my C# program. DateTime dateForButton = DateTime.Now; dateForButton

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.