Reputation: 2748
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers[0] = int.Parse(textBoxNum1.Text);
textBoxNum1 has a value of 5
This code gives the error
Index was out of range. Must be non-negative and less than the size of the collection.
Why ?
Upvotes: 2
Views: 8002
Reputation: 1887
creating a new List with
new List<int>();
creates a List with a 0 size (http://msdn.microsoft.com/en-us/library/4kf43ys3(v=vs.110).aspx).
With [0] you are trying to get the element at the Position 0. A List with zero-size does not have a 0 Index -> Index out of Range
See http://www.dotnetperls.com/list for examples on how to use List.
In this case it would be:
MyLottoNumbers.Add(int.Parse(textBoxNum1.Text));
Add does add the value at the end of the List.
yes but what if i have to add a value to a specific list value as illustrated ?
As it seems you want to add the value at a specific Index. If you know the size of the List at Design-time you should consider using a Array instead of a List.
Upvotes: 4
Reputation: 20909
If you read exception message, it says:
Index was out of range. Must be non-negative and less than the size of the collection
This happens because when created List has 0 elements length (examine MyLottoNumbers.Count). This is noted in constructor's summary:
public List() Member of System.Collections.Generic.List
Summary: Initializes a new instance of the System.Collections.Generic.List class that is empty and has the default initial capacity.
You're trying to use indexer:
public T this[int index] { set; get; } Member of System.Collections.Generic.List
Summary: Gets or sets the element at the specified index.
to set the element at position 0. However, you get an exception, because there's actually no element at position 0 yet (as the list is empty).
The List size increases when you execute MyLottoNumbers.Add
.
What you really need to do is:
MyLottoNumbers.Add(int.Parse(textBoxNum1.Text);
NB: This one of the differences between a List and an array: Lists are empty when created, whereas arrays are not. So you could rewrite your example using an array:
int[] MyLottoNumbers = new int[25];
MyLottoNumbers[0] = int.Parse("5");
Upvotes: 3
Reputation: 31596
Try this instead to create and parse the list using Regex and linq.
Note it assumes the user is putting in spaces to split on such as "5 12 15" as such:
if (string.IsNullOrWhiteSpace(textBoxNum1.Text) == false)
{
MyLottoNumbers = Regex.Matches(textBoxNum1.Text, @"([^\s]+)\s*")
.OfType<Match>()
.Select(mt => int.Parse(mt.Groups[0].Value))
.ToList();
}
else
{
MyLottoNumbers = new List<int>(); // Create empty list as to not throw an exception.
}
Upvotes: 3
Reputation: 5119
You need to .Add()
the value of your textbox to your List
. Like this:
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers.Add(int.Parse(textBoxNum1.Text);
When you new
up a List
, it's size is set to 0 until you .Add()
items to it.
Upvotes: 4
Reputation: 25370
It's because your list is currently empty, so you can't set the first index to something (because it doesn't exist). If you did this:
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers.Add(int.Parse("5"));
MyLottoNumbers[0] = int.Parse("7");
it works, because that index has been set.
If you want to insert at the front, take this route:
List<int> MyLottoNumbers = new List<int>();
MyLottoNumbers.Insert(0, int.Parse(textBoxNum1.Text));
Upvotes: 4