?
Solved

foreach index

Posted on 2009-12-31
5
Medium Priority
?
447 Views
Last Modified: 2012-05-08
When using foreach is it possible to get the index of the loop when certain condition become true?

For example:
foreach(MyObject o in oList){
     if(o.Id==5){
          //Find index of the oList
    }
}
0
Comment
Question by:johnkainn
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
Jon500 earned 1000 total points
ID: 26152725
No, there is not--for two reasons:

1) The object MyObject in oList is an object of type MyObject and not type oList and, as such, is not related in any way other than it is contained in the oList collection.

2) It is possible for multiple o.Id values to equal 5, in which case it is not possible to go back to see which o object was indexed at the time. Remember, once o is extracted from oList, it is simply object o.

If you need the index, it's better to iterate through the collection and look to see if the object of a particular index is of type o--and, if it is, examine it's o.Id property value.

Cheers,
Jon
0
 
LVL 22

Expert Comment

by:p_davis
ID: 26153497
i understand Jon's 2nd point -- you have to make that unique but it is possible to get the index of the object in the list.

Maybe i am misunderstanding?

i put together a little project with the loading of a list of MyObject and then a little loop that grabs the index of each object in that list.
public partial class Form1 : Form
    {
        List<MyObject> oList = new List<MyObject>();

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Random r = new Random(1);

            for (int i = 0; i <= 10; i++)
            {
                MyObject mo = new MyObject();
                mo.Id = r.Next(200);
                mo.Description = i.ToString();

                oList.Add(mo);
            }

            foreach (MyObject o in oList)
            {
                Int32 index = oList.IndexOf(o);
                //index now contains the index number of o in the oList.

            }
        }
    }

    public class MyObject
    {
        Int32 id;
        String description;

        public Int32 Id
        { get; set; }

        public string Description
        {
            get;
            set;
        }
    }

Open in new window

0
 
LVL 8

Expert Comment

by:Jon500
ID: 26154050
@p_Davis,

Perhaps I misunderstand your addition, but what the author was trying to do is obtain the index within the foreach iteration he provided in his example loop. Within that loop, as I had shown, there is not a way to get the index of oList to which o belongs. What I suggested he do, instead, was pretty much what you suggested.

Are we in agreement?

Cheers,
Jon
0
 
LVL 22

Expert Comment

by:p_davis
ID: 26154076
i need this holiday break =)

it appears we are in agreement.
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26154193
:)

Have a happy, healthy one.

Cheers,
Jon500
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question