locked
Array out of bounds error RRS feed

  • Question

  • I have the following code

    if ((linenumber >= 0) && (linenumber < lines->Count))
    	{
    
    		// Get the string out of the arraylist
    		String ^line = safe_cast<String^>(lines[linenumber]);
    
    		// Split it into various pieces on the space
    		array<String^> ^pieces = line->Split('\t');
    
    		if (pieces[0] == "end")
    		{
    			curRec += 2;
    			displayLine(curRec);
    		}
    		else
    		{
    			textBox1->Text = pieces[0];
    			textBox2->Text = pieces[1];
    			textBox3->Text = pieces[2];
    			textBox4->Text = pieces[3];;
    			
    		}
    I am getting an error that the pieces is out of the array bounds

    Sunday, February 27, 2011 12:44 AM

Answers

  • Hi HeartBreakKid_123,

    Prior to using the array (just after the line with the Split() function), check the number of elements in the returned pieces array with the Length member.
    You can then deal with the case where there are no elements or less than you expected (or more).

    Chris

    • Proposed as answer by Shane_B Sunday, February 27, 2011 2:34 AM
    • Marked as answer by Jie Bao Sunday, March 6, 2011 11:38 PM
    Sunday, February 27, 2011 2:23 AM
  • Check if curRec is truely mapping to the record you think it is especially at the calling of displayLine().  Recall that arrays start at 0.

    Also, please "mark as answer" since it appears that your original question was answered.
    Thanks,
    Chris

    • Marked as answer by Jie Bao Sunday, March 6, 2011 11:38 PM
    Monday, February 28, 2011 2:16 AM

All replies

  • Hi HeartBreakKid_123,

    Prior to using the array (just after the line with the Split() function), check the number of elements in the returned pieces array with the Length member.
    You can then deal with the case where there are no elements or less than you expected (or more).

    Chris

    • Proposed as answer by Shane_B Sunday, February 27, 2011 2:34 AM
    • Marked as answer by Jie Bao Sunday, March 6, 2011 11:38 PM
    Sunday, February 27, 2011 2:23 AM
  • Hey, I have modified that section to the below and it solves the problem:
    if (pieces[0] == "end")
    		{
    			curRec += 2;
    			displayLine(curRec);
    		}
    		else if (pieces->Length == 1)
    		{
    			if (curRec > 1)
    			{
    				curRec -= 2;
    				displayLine(curRec);
    			}
    		}
    		else
    		{
    			textBox1->Text = pieces[0];
    			textBox2->Text = pieces[1];
    			textBox3->Text = pieces[2];
    			textBox4->Text = pieces[3];
    		
    		}

    Only problem is I had a next and previous button but they are not moving the through the record.

    The code for the previous button is:

    if (curRec == 2)
    			{
    				this->button1->Enabled = false;
    			}
    			if (curRec > 0)
    			{
    				curRec -= 1;
    				displayLine(curRec);
    			}

    Code for the next button:

    if (curRec < lines->Count - 1)
    			{
    				curRec += 1;
    				displayLine(curRec);
    				this->button1->Enabled = true;
    			}
     They are not moving to next record

    Sunday, February 27, 2011 2:35 AM
  • You'll have to provide a lot more context as to what it is you're trying to accomplish.

     

    Sunday, February 27, 2011 2:45 AM
  • Basically I am writing user data to a text file in the manner of:

    Scit-1020 Physcis 1 4

    Cit-1234 Computer 3 4

    I want to now read that data back and display them in textboxes. Which I have accomplished above. In the same breadth I want to allow the user to view the records so I have two buttons previous and next that will allow the user to traverse the file to the next record and son.

     

    Sunday, February 27, 2011 2:51 AM
  • Check if curRec is truely mapping to the record you think it is especially at the calling of displayLine().  Recall that arrays start at 0.

    Also, please "mark as answer" since it appears that your original question was answered.
    Thanks,
    Chris

    • Marked as answer by Jie Bao Sunday, March 6, 2011 11:38 PM
    Monday, February 28, 2011 2:16 AM