简体   繁体   中英

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

I want to insert values received from database to a list.

public class Color
{
   public int ColorId {get;set;}
   public string ColorName {get;set;}
   public string ShortName {get; set;}
}


int index = 0;
List<Color> color = new List<Color>();

foreach(var items in output)
{
   color[index].ColorId = items.ColorId;       
// In this code, it doesn't insert the value of ColorId into color[0] position of list
// color[index].ColorId threw an exception of type 'System.ArgumentOutofRangeException' error
   color[index].ColorName = items.ColorName;
   color[index].ShortName = items.ShortName;
// some other codes 
// .........
index++;       // this is to insert values to list during another (second loop) of foreach statement above

}

But I get this error:

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

How can I insert the values properly? Please help.

Currently, you have an empty list, and you are attempting to edit the value at position index - but that value doesn't exist because your list is empty.

You need to add the items, and you can do this with the List.Add method to add to your list.

foreach(var item in output)
{
   // Note - I am assuming `item` is a `Color`. If not, you will need
   // to create a new Color with `var color = new Color()`
   color.Add(item);
}

You need to add items to your list, you cannot refer to them by index before you have done that. For example:

foreach(var items in output)
{
    var newColor = new Color
    {
        ColorId = items.ColorId,
        ColorName = items.ColorName,
        shortName = items.ShortName,
    };

    color.Add(newColor);

    // some other codes 
    // .........
    index++;       // this is to insert values to list during another (second loop) of foreach statement above

}

An easier way would be to use Linq to create the list:

List<Color> color = output
    .Select(o => new Color
    {
        ColorId = o.ColorId,
        ColorName = o.ColorName,
        shortName = o.ShortName,
    })
    .ToList();

The issue is that you're trying to edit rows in the List object that haven't been added yet, and therefore don't exist, and therefore the code fails.

foreach(var items in output)
{
   Color newItem = new Color()
   {
      ColorId = items.ColorId,
      ColorName = items.ColorName,
      shortName = items.ShortName,
    };
    color.add(newItem);
    index++;
}

Your List<Color> color = new List<Color>(); initializes a new instance of the color List object.

But in your foreach loop, you are using an index to it, but it has no members. That's why the error message.

Assuming your items is of type Color , use instead:

color.Add(item);

The reason for the error is that there is no item in the list at the value of index (ie 0 , the first time). List<Color> colorList = new List<Color>(); just creates an empty list with nothing in it. You can't assign values to an object which doesn't exist.

You need to create a blank Color object and then add it to the list. You don't need the index variable for that.

List<Color> colors = new List<Color>();

foreach(var items in output)
{
  Color col = new Color();
  col.ColorId = items.ColorId;       
  col.ColorName = items.ColorName;
  col.ShortName = items.ShortName;
  // .........
  colors.Add(col);
}

You also then don't need any kind of secondary loop (as mentioned in your code comments) to add items to the list - they are already added.

PS if output is already some kind of list of Color objects then likely you don't need this loop at all. You didn't mention what the type of that variable is.

Create new object of color , assign value to it, and finally add to list

List<Color> colorList = new List<Color>();
Color color;
foreach(var item in output)
{
   color = new Color();
   color.ColorId = item.ColorId;       
   color.ColorName = item.ColorName;
   color.ShortName = item.ShortName;
   colorList.Add(color); 
}

Cool as ICE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM