I have 9 objects declared, each object has an ID variable. I need to loop through all of the objects and verify whether the ID is equal to another variable. When i find the object it will be used for other things. Since objects are more complex than simple variables i don't know how to switch to the following one during the loop. Is it even possible? Are there any other ways to get to the same result? (using C#)
Asked
Active
Viewed 100 times
-4
-
1Use [`List.Find()`](https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.110).aspx)? Did you even try anything to solve this yourself? – UnholySheep May 13 '18 at 14:55
-
i tried looking on internet but couldn't find anything. So i tried using arrays with disastrous results. When i say that i have a list of objects i mean that i have just a bunch of objects declared, not a proper list. – Fabio R. May 13 '18 at 15:04
-
2Show your work so far. Are the objects of the same type or different types? What does "disastrous results" mean? – Eric Lippert May 13 '18 at 15:05
-
If you don't have "a proper list", *can* you store them in a proper list, or any other collection? Most collections either have instance methods or extension methods that can easily do exactly what you ask for. For instance, if you *did* have them in a list, a simple `list.FirstOrDefault(item => item.Id == thatOtherVariable);` would give you the element, or `null` in case it didn't find it. A general tip is if you're dealing with a collection of items (collection here meaning "a number of items"), it's actually best to *use* a collection (collection here meaning a data structure). – Lasse V. Karlsen May 13 '18 at 15:06
-
@EricLippert Yes, they are of the same type, they just differ from that ID variable. "Disastrous results" mean that the array couldn't even be created because the compiler didn't recognise the declaration. – Fabio R. May 13 '18 at 15:13
-
You have a rather different definition of "disastrous" than I do. Anyways, if your question is "how do I declare and initialize an array?" there's already a question on that. Is this question really a duplicate of https://stackoverflow.com/a/5678393/88656 ? – Eric Lippert May 13 '18 at 15:22
-
@EricLippert nono, i already discarded the idea of using arrays. I know how to declare them, the problem is another one. – Fabio R. May 13 '18 at 21:40
2 Answers
2
If you have objects with the same type you can put them in a list and after you just have to do a "for each"
List<MyObjectType> myObjectList = new List<MyObjectType>();
foreach (MyObjectType item in myObjectList)
{
if (item.Id == anOtherObject.Id)
{
#Do your job
}
}

Uwe Keim
- 39,551
- 56
- 175
- 291

Yoann Carrer
- 67
- 1
- 4
-
I tried this Code, but the foreach cycle uses always the same item 9 times and never changes. Why? – Fabio R. May 14 '18 at 10:52
0
If you need a one-line solution:
List<MyObjectType> myObjectList = new List<MyObjectType>();
MyObjectType result=myObjectList.FirstOrDefault(x=>x.Id== targetedId);

Tobias Brohl
- 479
- 6
- 25