Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
I get this error on MyData[0]

C#
protected void TextBoxFirstName_TextChanged(object sender, EventArgs e)
        {
            
            
                if (TextBoxFirstName.Text != string.Empty)
                {
                    MyData[0] = TextBoxFirstName.Text;
                }
            
        }

        

        protected void TextBoxLastName_TextChanged(object sender, EventArgs e)
        {

            if (TextBoxLastName.Text != string.Empty)
            {
                MyData[1] = TextBoxLastName.Text;
            }
        }


C#
private ArrayList _sharedData = new ArrayList();
       public ArrayList MyData
       {
           get { return _sharedData; }
           set { _sharedData = value; }
       }


C#
int i;
        public void MyData(ArrayList cvData)
        {
            for (i = 0; i <= cvData.Count - 1; i++)
            {
                cvData[i] = LabelFirstName.Text;
                cvData[i] = LabelPosition.Text;
                cvData[i] = LabelLastName.Text;
            }
            
            //LabelPosition.Text = cvData;
            //LabelLastName.Text = cvData;
        }
Posted
Comments
E.F. Nijboer 13-May-13 3:56am    
Place a breakpoint at the start and simply step through the code. Use a few watches to monitor some of the variables and you will surely find the error.
Mohammed Hameed 13-May-13 3:57am    
You have to make sure that MyData[0] and MyData[1] has some value but not null. Just debug and verify at runtime what are the values.

You are not setting the Array list to have any objects.

You should change your code to do something similar to this:

C#
if(MyData.Count <= 0) MyData.Add(TextBoxFirstName.Text);
else MyData[0] = TextBoxFirstName.Text;


in the last name event do something similar to:

C#
if(MyData.Count < 2) MyData.Add(TextBoxLastName.Text);
else MyData[1] = TextBoxLastName.Text;
 
Share this answer
 
v2
Comments
Kurac1 13-May-13 4:05am    
Doing like this i dont get any Error, but i dont get any result..
At the time
C#
MyData[0] = TextBoxFirstName.Text;
is executing, MyData[0] does not exist. This is often caused by declaring
C#
List<string> MyData = new List<string>(1024);
and then using MyData[0], MyData[1] and so on. If this is the case, you have to add elements to the list prior to assigning something to them (the 1024 is not the number of elements in the list, but the initial potential number of elements[^]).
 
Share this answer
 
v3
Comments
Kurac1 13-May-13 4:14am    
My problem is that i want to send data between two sharepoint webpart so TextBoxFirstName should be displayed in LabelFirstName and TextBoxLastName should be displayed in LabelLastName before when not using a ArrayList when i wrote something in TextBoxFirstName it got displayed in both LabelFirstName and LabelLastName, so i thoug i should use a arraylist?
lukeer 13-May-13 4:35am    
Concerning SharePoint, I'm totally uneducated. My proposal is based solely on the sparse information I could tate from the question. SharePoint is not mentioned there, not even MyData's declaration is included, so I had to guess it.

For two controls showing the same data, keep in mind Ian A Davidson's thorough analysis, especially part c).
1) Do you ever add any objects to the "MyData" ArrayList (_sharedData)?
2) You should check that "cvData" in your MyData method is not null and that it has elements.



A few other weird points with this code (bearing in mind that obviously there is a great deal not included in the question above):

a) You seem to have a property and a method with the same name (MyData). Make these names different and more meaningful.

b) For an indexed "for" loop, why not just use less-than, <, rather than less-than-or-equal, <=, and then subtracting by one? I.e.
C#
for (int i = 0; i < cvData.Count; ++i)
<d>Don't declare you index variable for this outside the loop (unless you are going to break out of the loop for some condition and need to know what value you're at - though this is rare), and certainly don't declare it outside of the method!

c) You seem to assign three different values to the same object in the loop:
C#
cvData[i] = LabelFirstName.Text;
cvData[i] = LabelPosition.Text;
cvData[i] = LabelLastName.Text;


d) Would you be better off using a strongly typed collection (e.g. List<T>, or Collection<T>) rather than ArrayList?

e) In respect of b,c and d, once you've sorted out what your loop should be doing, you might be able to consider using a "foreach" statement instead of the for loop:
C#
foreach (/*object*/ item in cvData)


Regards,
Ian.
 
Share this answer
 
v3
Comments
Kurac1 13-May-13 4:31am    
Using a List<string> i get the same value in both label the count is 1 only so it get same in all
Kurac1 13-May-13 4:32am    
public void MyData(List<string>cvData)
{
foreach (var vItem in cvData)
{
LabelFirstName.Text = vItem;
LabelPosition.Text = vItem;
LabelLastName.Text = vItem;

}

}

protected void TextBoxFirstName_TextChanged(object sender, EventArgs e)
{

if (TextBoxFirstName.Text != string.Empty)
{
MyData.Add(TextBoxFirstName.Text);
}
}



protected void TextBoxLastName_TextChanged(object sender, EventArgs e)
{

if (TextBoxLastName.Text != string.Empty)
{
MyData.Add(TextBoxLastName.Text);
}
}

protected void TextBoxPosition_TextChanged(object sender, EventArgs e)
{

if (TextBoxPosition.Text != string.Empty)
{
MyData.Add(TextBoxPosition.Text);
}

}

private List<string> _sharedData = new List<string>();
public List<string> MyData
{
get { return _sharedData; }
set { _sharedData = value; }
}

}

}
lukeer 13-May-13 4:44am    
1) Put this in some place where you can wrap a code block around it.
2) The three TextChanged events keep adding elements to MyData. That means that it will grow to infinity due to a user changing one of the texts.
3) The MyData() method assings the first element in cvData to each of three labels. Then, it assigns the second element to each of the labels. Then it assigns the third element and so on until it assigns the last element to all three labels.
Finally, all three labels seem to show the text that has been changed last.

4) It seems that you don't really need a List that can grow during usage, but instead a structure that can hold exactly three values.
Ian A Davidson 13-May-13 6:17am    
I meant use a strongly typed collection, like List<T>, not List. I.e. if you're using it to hold strings, how about using List<string>, or maybe StringCollection? I would suggest you rename your MyData method and property so that they don't have the same name (that is just bound to end in confusion!) However, these things in themselves is not going to fix the indexing issue you have. The code you supply here will add elements, but as Lukeer has pointed out, you'll end up with one every time you change the text (therefore including incomplete entries). How about a "submit" buton ("OK"?) to assign the change instead - or possibly detect the change in focus? Obviously I'm not sure what you're trying to achieve by added the text from a text box into a list! Do you really need to do this, bearing in mind that you have the text in the text boxes when OK will be clicked? I think you need to rethink what you are actually trying to do, do a bit of rework - as Lukeer has suggested - and then if all else fails, do some debugging. Regards, Ian.
Ian A Davidson 13-May-13 6:07am    
Thank you lukeer, for correcting my typo. :)
Ian.
your count limit could be too high in mydata method for loop.

check this link.

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index[^]
 
Share this answer
 
v2

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