By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

list of list of type int

P: 26
Dear all

I have created a list contains many lists of type int by using the following line


List<List<int>> values = new List<List<int>>();

then I tried to fill one int value at each list


for (int i = 0; i < running.Items.Count; i++) {
values[i].Add(Convert.ToInt32(running.Items[i].SubItems[1].Text.ToString()));

}

and then when I call the method again it fill the sec. value in the all lists

but I got that error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

what can I do
Feb 11 '09 #1
Share this Question
Share on Google+
1 Reply


vekipeki
Expert 100+
P: 229
Did you add the actual inner List<int> objects to your List<List<int>>? E.g. you should have something like this:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < running.Items.Count; i++)
  2. {
  3.     List<int> lst = new List<int>();
  4.     values.Add(lst);
  5. }
  6.  
You can probably do it on-the-fly (create a temporary List<int>, add your numbers inside, and that add the list to values), but from your code I didn't see where you iterate through the SubItems collection - but you should get the idea.
Feb 11 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.