tags:

views:

5517

answers:

9

Hi,

following is an extract from a code:

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

I am trying to use the following in my main () function code:

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

Problem is here: I am trying to print all elements in my List using a for loop:

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

I want to find the last element so that I equate cnt3 in my for loop and print out all entries in the List. Each element in the list is an object of the class AllIntegerIDs as mentioned above in the code sample. How do I find the last valid entry in the List?

should I use somthing like integerList.Find(integerList[].m_MessageText == null;

if i use that it will need an index that will range from 0 to watever maximum. Means I will hve to use another for loop which I do not intend to use. Is there a shorter/better way?

Thanks, Viren

+2  A: 

Try using a For Each instead of a For for lists. It will be a lot easier.

IPX Ares
+1  A: 

Change

for (int cnt3 = 0 ; cnt3 <= integerList.FindLastIndex ; cnt3++)

to

for (int cnt3 = 0 ; cnt3 < integerList.Count; cnt3++)
Eric J.
foreach is often more convenient to use, but is SLIGHTLY slower.
Eric J.
if using Count... do a -1 or you will get an index error.for (int cnt3 = 0 ; cnt3 < integerList.Count - 1; cnt3++)
IPX Ares
That's why I changed <= to <. The code is correct as posted :-)
Eric J.
@Eric: It used to be slower, but it's a trivial case to hit in the JIT so I'd be surprised if they haven't by now. :dunno:
280Z28
@IPX Ares: Seems to still be an issue, depending on the data type you are iterating:http://stackoverflow.com/questions/365615/in-c-which-loop-runs-faster-for-or-foreach#365622
Eric J.
A: 

Why not just use the Count property on the List?

for(int cnt3 = 0; cnt3 < integerList.Count; cnt3++)
Brandon
A: 

Use the Count property. The last index will be Count - 1.

for (int cnt3 = 0 ; cnt3 < integerList.Count; cnt3++)
Spencer Ruport
+3  A: 

If you just want to access the last item in the list you can do

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

to get the total number of items in the list you can use the Count propery

var itemCount = integerList.Count;
Jared
A: 
int lastInt = integerList[integerList.Count-1];
Dan Diplo
A: 

Thanks a lot all of you'll for your quick suggestions..the following worked perfectly:

for (int cnt3 = 0 ; cnt3 < integerList.Count; cnt3++)

Thanks, Viren

VP
Then you should mark the first answer that solved your problem as the answer, so the person who helped you out can get credit.
Joel Mueller
+1 for Joel-1 for Viren. Viren, this is not a message board, as Joel said, you need to mark the persons's answer as such.
Darien Ford
A: 

I would have to agree a foreach would be a lot easier something like

foreach(AllIntegerIDs allIntegerIDs in integerList)
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\n", allIntegerIDs.m_MessageID,
allIntegerIDs.m_MessageType,
allIntegerIDs.m_ClassID,
allIntegerIDs.m_CategoryID,
allIntegerIDs.m_MessageText);
}

Also I would suggest you add properties to access your information instead of public fields, depending on your .net version you can add it like public int MessageType {get; set;} and get rid of the m_ from your public fields, properties etc as it shouldnt be there.

Michael Ciba
A: 

Independent of your original question, you will get better performance if you capture references to local variables rather than index into your list multiple times:

AllIntegerIDs ids = new AllIntegerIDs();
ids.m_MessageID = (int)IntegerIDsSubstring[IntOffset];
ids.m_MessageType = (int)IntegerIDsSubstring[IntOffset + 1];
ids.m_ClassID = (int)IntegerIDsSubstring[IntOffset + 2];
ids.m_CategoryID = (int)IntegerIDsSubstring[IntOffset + 3];
ids.m_MessageText = MessageTextSubstring;
integerList.Add(ids);

And in your for loop:

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