Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-01-2007, 05:03 PM   #1 (permalink)
PigBenis
duh
 
PigBenis's Avatar
 
Join Date: May 2002
Location: Boiler Up
Posts: 601
-24 Internets
Send a message via AIM to PigBenis
count iteration in a foreach

hey guys is there anyway to get the iteration number in a foreach loop in C# without having to create another variable?
PigBenis is offline   Reply With Quote
Old 12-01-2007, 09:45 PM   #2 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,634
+7 Internets
Quote:
Originally Posted by PigBenis View Post
hey guys is there anyway to get the iteration number in a foreach loop in C# without having to create another variable?
No. Use for.

Last edited by Fog : 12-01-2007 at 09:47 PM.
Fog is offline   Reply With Quote
Old 12-03-2007, 03:09 PM   #3 (permalink)
tikkus
Banned
 
Join Date: Nov 2003
Posts: 1,219
-3 Internets
In Perl I believe you can just use the base array/hash and it treats it like the number of elements. Not sure about C#.
tikkus is offline   Reply With Quote
Old 12-03-2007, 03:14 PM   #4 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 614
+0 Internets
Send a message via AIM to Zippygoose
Like a lot of others (probably) I went about trying to be clever with a way to do it in C# but there honestly isn't one that makes sense or is at all efficient. To access the iterator you should use for because foreach isn't made for that.
Zippygoose is offline   Reply With Quote
Old 12-03-2007, 06:57 PM   #5 (permalink)
Hachima
Registered User
 
Join Date: Oct 2004
Posts: 1,698
Here are two proof of concepts in one.

Ontopic one first:
All foreach needs is an object that implements IEnumberable. This means you are never guaranteed the object is indexable, or even know how many iterations will take place before the loop starts. As an example here is a program that uses foreach to write numbers from an object that just generates random numbers. The foreach will randomly terminate when a random number <= 20 is generated. There could be actual data structures where indexes don't make sense either.

Second thing this code shows:
The way I generate a series of random numbers is actually not random at all. Creating Random objects real close to each other will actually use the same seed and you will get a sequence of the exact same numbers if using the same range. So say you have a class for mobs that start with semi random stats. If they all create their own random number generator they will be a high chance they will start with the same stats. The correct way to do it is use a static random object (the commented out way) This way a new seed is used for each new random number since the .Next method generates a new seed.

Code:
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace RandomNums { public class RandomNums : IEnumerable { static Random rand = new Random(); public IEnumerator GetEnumerator() { return new RandEnumerator(); } public class RandEnumerator : IEnumerator { int number; public object Current { get { return number; } } public bool MoveNext() { return ((number = new Random().Next(100)) > 20); //return ((number = rand.Next(100)) > 20); } public void Reset() { } } } class Program { static void Main(string[] args) { RandomNums myRands = new RandomNums(); foreach (int num in myRands) { Console.WriteLine(num); } } } }
Hachima is offline   Reply With Quote
Old 12-04-2007, 02:12 AM   #6 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Like Hachima so nicely showed, a Iterator can not give you a index nor can it find a specific object at a certain index. The reason is simple.
Iterator is just a object that goes through a collection to give you a "next" object. It's supposed to work on all collections, even randomAccessCollections, Sets etc, where the order is irrelevant, it can't give you a index since that would make the method or function invalid for other type of collections. Foreach loop uses a iterator, so you're not supposed to get the index when using a for each. If you want the index, use a normal for loop, since that is your own implementation.
slitz is offline   Reply With Quote
Old 12-04-2007, 10:18 AM   #7 (permalink)
Zippygoose
Math Enthusiast/Badass MC
 
Zippygoose's Avatar
 
Join Date: Jun 2002
Location: Seattle
Posts: 614
+0 Internets
Send a message via AIM to Zippygoose
Quote:
Originally Posted by slitz View Post
Like Hachima so nicely showed, a Iterator can not give you a index nor can it find a specific object at a certain index. The reason is simple.
Iterator is just a object that goes through a collection to give you a "next" object. It's supposed to work on all collections, even randomAccessCollections, Sets etc, where the order is irrelevant, it can't give you a index since that would make the method or function invalid for other type of collections. Foreach loop uses a iterator, so you're not supposed to get the index when using a for each. If you want the index, use a normal for loop, since that is your own implementation.
I am filing this under "great interview questions" btw.
Zippygoose is offline   Reply With Quote
Old 12-04-2007, 02:32 PM   #8 (permalink)
Fog
Registered User
 
Join Date: Feb 2006
Posts: 1,634
+7 Internets
Quote:
Originally Posted by Zippygoose View Post
I am filing this under "great interview questions" btw.
In my intro C# CS classes I found that one of the most pervasive mistakes was people who overused foreach, even people who were not new to programming. A lot of people would use a foreach loop to enumerate things like elements of an array, just because it was a handy shortcut to getting a reference to each item, in situations where they actually needed to get each element in order by index.

Often, it would be an invisible bug, because the code would work properly. Most of Microsoft's collections like that will return their elements in order if you enumerate them with a foreach. It's awful practice though because there's no guarantee that they will enumerate them the same way twice; in the next version of the .NET framework your favorite foreach might spit out items in a different order and break your code. Not good.
Fog is offline   Reply With Quote
Old 12-04-2007, 02:34 PM   #9 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Quote:
Originally Posted by Fog View Post
In my intro C# CS classes I found that one of the most pervasive mistakes was people who overused foreach, even people who were not new to programming. A lot of people would use a foreach loop to enumerate things like elements of an array, just because it was a handy shortcut to getting a reference to each item, in situations where they actually needed to get each element in order by index.

Often, it would be an invisible bug, because the code would work properly. Most of Microsoft's collections like that will return their elements in order if you enumerate them with a foreach. It's awful practice though because there's no guarantee that they will enumerate them the same way twice; in the next version of the .NET framework your favorite foreach might spit out items in a different order and break your code. Not good.
That's people not understanding iterators though or the collection they are using. Never ever use a collection that requires a iterator, if order matters, if you want index or grab a object at a certain index when you can only get a iterator, you're using the wrong collection.

Last edited by slitz : 12-04-2007 at 02:45 PM.
slitz is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 02:48 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6