473,581 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Last row in foreach loop

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 RaceCarCollecti on)
{
...

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
Feb 29 '08 #1
23 29987
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 RaceCarCollecti on)
{
...

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 RaceCarCollecti on.Count (or .Length)

Why not use a for loop instead?
--
Bjørn Brox
Feb 29 '08 #2
On Feb 29, 4:02*pm, "tshad" <ts...@dslextre me.comwrote:
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 RaceCarCollecti on)
{
* * ...

* * 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.Coun t; i++)
{
object = collection[i];
...
if (i == collection.Coun t - 1)
do something to the last object
}
Feb 29 '08 #3
Doubt it. I've always use a counter variable and compared to the length of
the collection. Not a big deal.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net
"tshad" wrote:
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 RaceCarCollecti on)
{
...

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
Feb 29 '08 #4
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 RaceCarCollecti on)
{
lastRacecar = racecar;

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

"tshad" <ts***@dslextre me.comwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
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 RaceCarCollecti on)
{
...

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

Feb 29 '08 #5
On 29 Feb, 22:02, "tshad" <ts...@dslextre me.comwrote:
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 RaceCarCollecti on)
{
* * ...

* * 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$ SQLserverExpres s 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
Feb 29 '08 #6
On Feb 29, 4:17*pm, za...@construct ion-imaging.com wrote:
On Feb 29, 4:02*pm, "tshad" <ts...@dslextre me.comwrote:


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 RaceCarCollecti on)
{
* * ...
* * 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.Coun t; i++)
{
* * object = collection[i];
* * ...
* * if (i == collection.Coun t - 1)
* * * * do something to the last object

}
Sorry, that should be for instead of foreach.
Feb 29 '08 #7

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

IEnumerator<Rac eCare = RaceCarCollecti on.GetEnumerato r();
e.Reset();
bool isValid = e.MoveNext();
while (isValid)
{
RaceCar car = e.Current;
isValid = e.MoveNext();
if ( !isValid )
{
// this is the last car
}
}
tshad wrote:
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 RaceCarCollecti on)
{
...

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

Feb 29 '08 #8


"tshad" wrote:
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 RaceCarCollecti on)
{
...

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 == RaceCarCollecti on [RaceCarCollecti on.count - 1])
{
// do something...
}
>
Mar 1 '08 #9
On Mar 1, 1:50 am, Family Tree Mike
<FamilyTreeM... @discussions.mi crosoft.comwrot e:
Won't the following work? I can't say it's efficient...

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

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

Jon
Mar 1 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
15933
by: Mike P | last post by:
In a foreach loop, how do you break out of the iteration but not the loop? Break and continue both seem to exit the loop, I need something that for example on an if statement will cease execution of the current iteration and move to the next one. Any help would be really appreciated. Cheers,
13
14454
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in findListBox.listBox2.Items) { listBox2.Items count changed, restart this foreach } Thanks for any help.
18
6614
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset the loop. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
5
2643
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) { Console.WriteLine(s.StudentID); }
3
1997
by: cody | last post by:
Currently it is only legal to use types which has a method named GetEnumerator to be used in a foreach loop. This makes it impossible to use the same Enumerator after and before a foreach loop, because GetEnumerator always returns a new one. My proposal would allow the following scenario: You could for example the first and the last...
10
2927
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a generic list of business objects. I'm able to see that the type is the correct one in the debugger. However when I try to traverse the list using the...
1
1858
by: Perl Beginner | last post by:
I hope i can articulate this question properly. i have been trying to figure this out for over a week. I am comparing the contents of two files, but the comparison is done inside of a foreach loop (the code that i have so far is below). At the instance the files are different, i want to capture one of the files and use it later on in the...
3
2670
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
1
3354
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of the iteration variable is used and you cannot modify it. For example: int pins = {9, 3, 7, 2} int newPin = 10; foreach(int pin in pins) {
0
7789
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8301
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8169
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6551
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5361
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3820
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1132
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.