Last row in foreach loop

T

tshad

Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
...

if last row do something?
}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Thanks,

Tom
 
B

Bjørn Brox

tshad skrev:
Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
...

if last row do something?
}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?
Not without counying an test against RaceCarCollection.Count (or .Length)

Why not use a for loop instead?
 
Z

zacks

Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
    ...

    if last row do something?

}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Thanks,

Tom

Not as far as I am aware of. I have done someting like this with:

foreach (int i = 0; i < collection.Count; i++)
{
object = collection;
...
if (i == collection.Count - 1)
do something to the last object
}
 
R

Rene

Besides what others have mentioned, here is another way to tackle your
problem without having to have a counter in case you had something against
counters.


Racecar lastRacecar;
foreach (Racecar racecar in RaceCarCollection)
{
lastRacecar = racecar;

// Do something...........
}
if(lastRacecar != null)
{
// Do last row stuff here...........
}
 
C

christery

Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
    ...

    if last row do something?

}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Thanks,

Tom

Sounds like you should test doing that with a database, the price for
MySQL or if you want to go M$ SQLserverExpress is nice (0$), a
pseudostatement like "readline(); if next line is EOF..." is not too
hard to program logically but you get double the IO, and for
nothing.. or as mentioned check the length..
//CY
 
Z

zacks

Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?
foreach (Racecar racecar in RaceCarCollection)
{
    ...
    if last row do something?

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Tom

Not as far as I am aware of. I have done someting like this with:

foreach (int i = 0; i < collection.Count; i++)
{
    object = collection;
    ...
    if (i == collection.Count - 1)
        do something to the last object



}


Sorry, that should be for instead of foreach.
 
D

Darren

You can get the enumerator then use Reset, Current, & MoveNext but it's ugly.

IEnumerator<RaceCar> e = RaceCarCollection.GetEnumerator();
e.Reset();
bool isValid = e.MoveNext();
while (isValid)
{
RaceCar car = e.Current;
isValid = e.MoveNext();
if ( !isValid )
{
// this is the last car
}
}
 
F

Family Tree Mike

tshad said:
Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
...

if last row do something?
}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Thanks,

Tom

Won't the following work? I can't say it's efficient...

if (racecar == RaceCarCollection [RaceCarCollection.count - 1])
{
// do something...
}
 
J

Jon Skeet [C# MVP]

Won't the following work? I can't say it's efficient...

if (racecar == RaceCarCollection [RaceCarCollection.count - 1])
{
// do something...

}

That entirely depends on the type of RaceCarCollection. If it's just
an IEnumerable<T>, it doesn't have a Count property.

Jon
 
P

Peter Morris

What occurs to me is that you only want something done once, so why put it
in the loop at all?

foreach (RaceCar currentRaceCar in RaceCarCollection)
currentRaceCar.Start();

RaceCarCollection[RaceCarCollection.Count - 1].BreakDown();
 
J

Jon Skeet [C# MVP]

Søren Reinke said:
Nice :)
'When C# 3 is released, this pattern will be made even simpler with'

Any chance for that update ? :)

The implicit typing is something I don't need to do anything with, but
I'll put in the extension method when I get a bit of time - thanks for
the reminder :)
 
S

Søren Reinke

Jon said:
The implicit typing is something I don't need to do anything with, but
I'll put in the extension method when I get a bit of time - thanks for
the reminder :)

:)

No problem.

I just enjoy getting tips from your website :)

See ya
Søren Reinke
 
F

Family Tree Mike

Jon Skeet said:
Won't the following work? I can't say it's efficient...

if (racecar == RaceCarCollection [RaceCarCollection.count - 1])
{
// do something...

}

That entirely depends on the type of RaceCarCollection. If it's just
an IEnumerable<T>, it doesn't have a Count property.

Jon

I see your point in earlier versions, but under 2008, IEnumerable<T>
supports a Count() method, but not a property. The original poster said they
knew the count though.
 
J

Jon Skeet [C# MVP]

Family Tree Mike said:
I see your point in earlier versions, but under 2008, IEnumerable<T>
supports a Count() method, but not a property.

Well, it sort of does. It has an extension method of Count() - but
unless the iterator is actually an IList it's going to retrieve that
count by iterating through the whole collection. Not exactly efficient.
The original poster said they knew the count though.

Ah, true.
 
T

tshad

Bjørn Brox said:
tshad skrev:
Not without counying an test against RaceCarCollection.Count (or .Length)

Why not use a for loop instead?

I could.

But it wasn't a problem just a question.

At the moment, I just set iktr= 0 and just a line after the foreach line
(iktr++;) and then test against RaceCarCollection.Count as you mentioned.

I was just curious if there was a property somewhere that told you what line
in the collection you were on without having to do this.

Thanks,

Tom
 
T

tshad

Is there a way to know if you are looking at the last record record of
foreach loop other then setting up a loop counter that you manually
increment?

foreach (Racecar racecar in RaceCarCollection)
{
...

if last row do something?

}

You can tell how many items you have in the collection but is there way to
tell which row you are looking or if this is the last row?

Thanks,

Tom

Not as far as I am aware of. I have done someting like this with:
foreach (int i = 0; i < collection.Count; i++)
{
object = collection;
...
if (i == collection.Count - 1)
do something to the last object
}


Something like this is fine but I might just as well do:

int iKtr = 0;
foreach (Racecar racecar in RaceCarCollection)
{
iKtr++;
...

if( iKtr == RaceCarCollection.Count)
do something?
}

Thanks,

Tom
 
T

tshad

Peter Bromberg said:
Doubt it. I've always use a counter variable and compared to the length of
the collection. Not a big deal.

I do the same. And as you said, no big deal.

I was just curious if there was a better way.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top