SORU
7 AĞUSTOS 2009, Cuma


Nasıl Listesi< son öğe bulabilirim;> ?

Aşağıdaki benim kod özü bir

public class AllIntegerIDs 
{
    public AllIntegerIDs() 
    {            
        m_MessageID = 0;
        m_MessageType = 0;
        m_ClassID = 0;
        m_CategoryID = 0;
        m_MessageText = null;
    }

    ~AllIntegerIDs()
    {
    }

    public void SetIntegerValues (int messageID, int messagetype,
        int classID, int categoryID)
    {
        this.m_MessageID = messageID;
        this.m_MessageType = messagetype;
        this.m_ClassID = classID;
        this.m_CategoryID = categoryID;
    }

    public string m_MessageText;
    public int m_MessageID;
    public int m_MessageType;
    public int m_ClassID;
    public int m_CategoryID;
}

Benim main aşağıdaki kullanmaya çalışıyorum() fonksiyonu kodu:

List<AllIntegerIDs> integerList = new List<AllIntegerIDs>();

/* some code here that is ised for following assignments*/
{
   integerList.Add(new AllIntegerIDs());
   index  ;
   integerList[index].m_MessageID = (int)IntegerIDsSubstring[IntOffset];
   integerList[index].m_MessageType = (int)IntegerIDsSubstring[IntOffset   1];
   integerList[index].m_ClassID = (int)IntegerIDsSubstring[IntOffset   2];
   integerList[index].m_CategoryID = (int)IntegerIDsSubstring[IntOffset   3];
   integerList[index].m_MessageText = MessageTextSubstring;
}

Sorun burada: listeme döngü için kullanarak tüm elemanları yazdırmak için çalışıyorum

for (int cnt3 = 0 ; cnt3 <= integerList.FindLastIndex ; cnt3  ) //<----PROBLEM HERE
{
   Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\n", integerList[cnt3].m_MessageID,integerList[cnt3].m_MessageType,integerList[cnt3].m_ClassID,integerList[cnt3].m_CategoryID, integerList[cnt3].m_MessageText);
}

Ben döngü için benim cnt3 günleriyle son elemanı bulmak istiyorum ve tüm öğeleri yazdırın. Listedeki her elemanı yukarıdaki kod örneğinde belirtildiği gibi sınıf AllİntegerİDs bir nesnedir. Nasıl Listesinde son geçerli giriş bulabilirim?

İntegerList gibi bir şey kullanmalıyım.([] İntegerList.m_MessageText == null;

Eğer bunu kullanırsam ne olursa olsun en fazla 0 arasında değişecek bir dizin gerekir. Kullanmak gibi bir niyetim yok döngü için başka bir kullanım için alacağım demektir. Daha kısa/daha iyi bir yolu var mı?

Teşekkürler Viren

CEVAP
7 AĞUSTOS 2009, Cuma


Eğer sadece listedeki son öğeye erişmek istiyorsanız yapabilirsiniz

var item = integerList[integerList.Count - 1];

listedeki öğelerin toplam sayısını almak için Count özellikli kullanabilirsiniz

var itemCount = integerList.Count;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • PorterRobinsonVEVO

    PorterRobins

    11 Kasım 2013
  • Top10Series

    Top10Series

    26 Kasım 2008
  • tseyina

    tseyina

    2 AĞUSTOS 2006