Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Index Out of Range

Index Out of Range

Scheduled Pinned Locked Moved C#
cssdatabasehelp
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I ignrod

    Difficult to know without all the source code. However, from your description, seems that variable i in function AddText is out of range. You should check that i is positive and less than list1.Length. Something like this

    //Function to add string dynamically in Class1
    private List list1 = new List();
    
    public void AddText(string myText) {
    	if ((i < 0) || (i >= list1.Length)) {
    		MessageBox.Show(String.Format("i is {0}. Should be between 0 and {1}", i, list1.Length));
    		return;
    	}
    	if((list1\[i\] == "") && (k < 1)) {
    		k = i;
    		list1\[i\] = myText;
    		k++;
    	}
    }
    

    By the way, please mind the ampersand in line

    	if((list1\[i\] == "") && (k < 1)) {
    

    Also, if you are looping, shouldn't there be a loop somewhere? I can't see it.

    A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #3

    Hi, thanks for replying. I forgot to include a for loop in my code, so here it is. public void AddText(string myText) { for (int i = 0; i

    A T 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, I 'm trying to loop through a List to see which element in the List is empty, then add strings entered via a text box into the empty element. I tried to display the content of the List element to check what it contains. I Get the error "Index was out of range. Must be non-negative and less than the size of the collection". The following is my code: //Function to add string dynamically in Class1 private List list1 = new List(); public string AddText(string myText) { if(list1[i]=="" & k<1) { k = i; list1[i] = myText; k++; } return list1[i]; } //Calling AddText() function from Class2 private void btn8_Click(object sender, EventArgs e) { string myText = txtDisplay.Text; ExpressionEval k = new ExpressionEval(); string g = k.AddText(myText); txtDisplay.Text = g; } Please point out why I'm getting this error, thanks in advance.

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #4

      Try debugging and stepping through your source code. It will help you figure out the exact line where you are getting this error. Further, based on this, you will be able to diagnose this error. Looks like the list1 list count is going wrong.

      Build your own survey - http://www.factile.net

      1 Reply Last reply
      0
      • A ASPnoob

        Hi, thanks for replying. I forgot to include a for loop in my code, so here it is. public void AddText(string myText) { for (int i = 0; i

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #5

        Based on this, try for (int i = 0; i Build your own survey - [http://www.factile.net](http://www.factile.net)

        1 Reply Last reply
        0
        • A ASPnoob

          Hi all, I 'm trying to loop through a List to see which element in the List is empty, then add strings entered via a text box into the empty element. I tried to display the content of the List element to check what it contains. I Get the error "Index was out of range. Must be non-negative and less than the size of the collection". The following is my code: //Function to add string dynamically in Class1 private List list1 = new List(); public string AddText(string myText) { if(list1[i]=="" & k<1) { k = i; list1[i] = myText; k++; } return list1[i]; } //Calling AddText() function from Class2 private void btn8_Click(object sender, EventArgs e) { string myText = txtDisplay.Text; ExpressionEval k = new ExpressionEval(); string g = k.AddText(myText); txtDisplay.Text = g; } Please point out why I'm getting this error, thanks in advance.

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #6

          Try debugging and stepping through your source code. It will help you figure out the exact line where you are getting this error. Further, based on this, you will be able to diagnose this error.

          Build your own survey - http://www.factile.net

          1 Reply Last reply
          0
          • A ASPnoob

            Hi all, I 'm trying to loop through a List to see which element in the List is empty, then add strings entered via a text box into the empty element. I tried to display the content of the List element to check what it contains. I Get the error "Index was out of range. Must be non-negative and less than the size of the collection". The following is my code: //Function to add string dynamically in Class1 private List list1 = new List(); public string AddText(string myText) { if(list1[i]=="" & k<1) { k = i; list1[i] = myText; k++; } return list1[i]; } //Calling AddText() function from Class2 private void btn8_Click(object sender, EventArgs e) { string myText = txtDisplay.Text; ExpressionEval k = new ExpressionEval(); string g = k.AddText(myText); txtDisplay.Text = g; } Please point out why I'm getting this error, thanks in advance.

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #7

            Pardon my ignorance, but I don't see where k gets initialized. It gets a little confusing, too, when you use letters for different things. In class 1, it's an integer; in class 2 it's something else.

            Will Rogers never met me.

            A A 2 Replies Last reply
            0
            • R Roger Wright

              Pardon my ignorance, but I don't see where k gets initialized. It gets a little confusing, too, when you use letters for different things. In class 1, it's an integer; in class 2 it's something else.

              Will Rogers never met me.

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #8

              Good points. I,j,k are quite confusing.

              Build your own survey - http://www.factile.net

              1 Reply Last reply
              0
              • R Roger Wright

                Pardon my ignorance, but I don't see where k gets initialized. It gets a little confusing, too, when you use letters for different things. In class 1, it's an integer; in class 2 it's something else.

                Will Rogers never met me.

                A Offline
                A Offline
                ASPnoob
                wrote on last edited by
                #9

                Hi, thank you all for responding. K is initialized outside of the function and I have modified my function as follows: class Class1 { private int k = 0; private List list1 = new List(); //Passing in myTemp as a string public string AddText(string myText) { int i = 0; do { //check to see if element is empty if (list1.Count == 0) { //Add the content of myText into list1 list1.Add(myText); i++; } } while (i < list1.Count); //Returns the content of the elements that were affected return list1[i]; } } I have stepped through the function and Visual Studio says the line, return list1[i];is the cause of the error message "Index was out of range". I don't know what else to do. If you can explain why I'm getting the error by looking at my code, please point it out. Thanks for your help.

                R T 2 Replies Last reply
                0
                • A ASPnoob

                  Hi, thank you all for responding. K is initialized outside of the function and I have modified my function as follows: class Class1 { private int k = 0; private List list1 = new List(); //Passing in myTemp as a string public string AddText(string myText) { int i = 0; do { //check to see if element is empty if (list1.Count == 0) { //Add the content of myText into list1 list1.Add(myText); i++; } } while (i < list1.Count); //Returns the content of the elements that were affected return list1[i]; } } I have stepped through the function and Visual Studio says the line, return list1[i];is the cause of the error message "Index was out of range". I don't know what else to do. If you can explain why I'm getting the error by looking at my code, please point it out. Thanks for your help.

                  R Offline
                  R Offline
                  Roger Wright
                  wrote on last edited by
                  #10

                  That's a good clue! Now, add list1.Count and i to the Watch list and step through again, observing how each line of code affects them. I think you'll find that, when you hit the return statement, i = list1.Count. That will put it out of range, since the last element of list1 has an index of Count - 1. Just a guess... :)

                  Will Rogers never met me.

                  1 Reply Last reply
                  0
                  • A ASPnoob

                    Hi, thanks for replying. I forgot to include a for loop in my code, so here it is. public void AddText(string myText) { for (int i = 0; i

                    T Offline
                    T Offline
                    Trak4Net
                    wrote on last edited by
                    #11

                    In this example I imaging that you would receive the index out of range at the return list1[k]; If you notice inside your for loop you assign k to the value of i then assign the value to the list, then you increment k by one value. this could work if you change the return list1[k] to return list1[k-1] (however you would also want to add some extra code to verify "k" is still within the index range of the list.

                    1 Reply Last reply
                    0
                    • A ASPnoob

                      Hi, thank you all for responding. K is initialized outside of the function and I have modified my function as follows: class Class1 { private int k = 0; private List list1 = new List(); //Passing in myTemp as a string public string AddText(string myText) { int i = 0; do { //check to see if element is empty if (list1.Count == 0) { //Add the content of myText into list1 list1.Add(myText); i++; } } while (i < list1.Count); //Returns the content of the elements that were affected return list1[i]; } } I have stepped through the function and Visual Studio says the line, return list1[i];is the cause of the error message "Index was out of range". I don't know what else to do. If you can explain why I'm getting the error by looking at my code, please point it out. Thanks for your help.

                      T Offline
                      T Offline
                      Trak4Net
                      wrote on last edited by
                      #12

                      The reason this code would throw an out of range exception is if the code executes within this section

                                        if (list1.Count == 0)
                                        {
                                              //Add the content of myText into list1
                                              list1.Add(myText);
                                              i++;
                                        }
                      

                      now you have added an item to the list and the count is now 1. You then increment i from zero to 1. Now you are trying to return the first item in the collection (a zero based index collection) with the value of 1 when the value you should be requesting is 0 return list1[i-1] or list1[0] or list1[list1.Count - 1] etc. of course if your i value is 0 and you try list1[i-1] you will also receive an error because that would be the value -1 and that is out of range. Hope that helps if you haven't already figured this out.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups