2011-10-07 9 views
5

Próbuję dodać listę w pętli for.Indeks był poza zakresem. Musi być nieujemny i mniejszy niż rozmiar kolekcji

Oto mój kod stworzyłem nieruchomości tutaj

public class SampleItem 
{ 
    public int Id { get; set; } 
    public string StringValue { get; set; } 
} 

chcę dodać wartość z innej listy

List<SampleItem> sampleItem = new List<SampleItem>(); // Error: Index out of range 
for (int i = 0; i < otherListItem.Count; i++) 
{ 
     sampleItem[i].Id = otherListItem[i].Id; 
     sampleItem[i].StringValue = otherListItem[i].Name; 
} 

Czy ktoś może naprawić mój kod proszę.

Odpowiedz

5

Otrzymujesz indeks poza zakresem, ponieważ odwołujesz się do sampleItem[i], gdy sampleItem nie ma żadnych pozycji. Musisz Add() elementy ...

List<SampleItem> sampleItem = new List<SampleItem>(); 
for (int i = 0; i < otherListItem.Count; i++) 
{ 
    sampleItem.Add(new SampleItem { 
     Id = otherListItem[i].Id, 
     StringValue = otherListItem[i].Name 
    }); 
} 
+0

Wow! Dzięki chłopaki. Tylko minutę i otrzymałem 8 odpowiedzi! Właśnie dlatego uwielbiam to miejsce! Spróbowałem i działa jak urok :) – HardCode

0
List<SampleItem> sampleItem = new List<SampleItem>(); // Error: Index out of range 
for (int i = 0; i < otherListItem.Count; i++) 
{ 
    sampleItem.Add(new sampleItem()); // add this line 
    sampleItem[i].Id = otherListItem[i].Id; 
    sampleItem[i].StringValue = otherListItem[i].Name; 
} 
0

List musi być Add ed do; nie można po prostu ustawić indeksowanych elementów na wartości, jeśli nie zostały jeszcze utworzone. Trzeba coś takiego:

List<SampleItem> sampleItems = new List<SampleItem>(); 
for (int i = 0; i < otherListItem.Count; i++) 
{ 
    SampleItem si = new SampleItem 
    { 
     Id = otherListItem[i].Id, 
     StringValue = otherListItem[i].Name 
    }; 
    sampleItems.Add(si); 
} 
0
List<SampleItem> sampleItem = new List<SampleItem>(); 
foreach(var item in otherListItem) 
{ 
sampleItem.Add(new SampleItem { Id = item.Id, StringValue = item.Name}); 
} 
0

w twojej pętli spróbuj wymienić co masz z czymś takim:

SampleItem item; 
item.Id = otherListItem[i].Id; 
item.StringValue = otherListItem[i].StringValue; 
sampleItem.add(item); 
0

użytku

List<SampleItem> sampleItem = (from x in otherListItem select new SampleItem { Id = x.Id, StringValue = x.Name }).ToList(); 
0

są następujące:

List<SampleItem> sampleItem = new List<SampleItem>(); 
for (int i = 0; i < otherListItem.Count; i++) 
{ 
     sampleItem.Add(new SampleItem {Id= otherListItem[i].Id, StringValue=otherListItem[i].Name}); 

} 
0

pojawia się błąd, ponieważ nigdy nie dodaje się żadnych elementów do listy sampleItem.

lepszym sposobem osiągnięcia tego celu byłoby przy użyciu LINQ (niesprawdzone)

var sampleItem = otherListItem.Select(i => new SampleItem { Id= i.Id, StringValue = i.Name}).ToList(); 
0

// używając system.linq;

otherListItem.ToList().Foreach(item=>{ 
    sampleItem.Add(new sampleItem{ 
}); 
0

Stało się tak, ponieważ dwukrotnie odwzorowałem kolumnę w klasie Mapper. W moim przypadku po prostu przypisałem elementy listy. np.

itemList item; 
ProductList product; 
item.name=product.name; 
item.price=product.price; 
Powiązane problemy