2011-10-07 13 views
5

Koleksiyonunun boyutu negatif ve negatif olmalıdır, for döngüsünde bir liste eklemeye çalışıyorum.Dizin aralık dışıydı.

İşte

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

Birisi kodumu lütfen düzeltin Can başka bir listeye

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; 
} 

değeri eklemek istediğiniz burada bir mülk yarattı kodum olduğunu.

cevap

5

sampleItem öğesinin hiçbir değeri olmadığında sampleItem[i] numaralı telefonu kullanacağınız için bir aralık dışı dizininiz var demektir. Sen Add() ürün ...

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! Teşekkürler beyler. Sadece bir dakika ve 8 cevabı aldım! Bu yüzden burayı seviyorum! Denedim ve bir çekicilik gibi çalışıyor :) – 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

A ListAdd ed olmalıdır olmalıdır; Henüz oluşturulmamışsa, endekslenmiş öğelerini değerlere ayarlayamazsınız.

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

: Sen gibi bir şey gerekiyor

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

kullanımı: döngü böyle bir şey ile ne varsa yerine deneyin yılında

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

aşağıdakileri yapın

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

, sampleItem listesine hiç öğe eklemediğiniz için hatayı alırsınız. Bunu yapmanın daha iyi bir yolu (denenmemiş) Linq kullanarak olurdu

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

// using System.Linq;

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

Bu bana göre, Mapper sınıfında iki kez bir sütun eşlemiştim. Benim durumumda sadece liste elemanlarını atadım. , ör.

itemList item; 
ProductList product; 
item.name=product.name; 
item.price=product.price;