+ 1
C# Finding typeof(UserControl) in a Collection
I was wondering if there is a simplification - if there is one. https://code.sololearn.com/c697M7ArID0s/?ref=app I am indexing through a list of UserControls. I want to find certain Custom UserControls by datatype. This line is invalid. I am wondering if there is something similar so that I don't have to use a for loop. int index = controls.IndexOf(typeof(Navigation));
5 Answers
+ 3
Your are looking for the "is" operator.
Console.WriteLine("The are {0} controls in the controlsList",Ā controlsList.Count);foreach(UserControl item in controlsList){Ā Ā if(item is Navigation) {Ā Ā Ā Ā Ā Ā Console.WriteLine("This is a navigation user control");Ā Ā Ā Ā Ā Ā item.Visible = true;Ā Ā }Ā Ā elseĀ Ā {Ā Ā Ā Ā Ā Ā Console.WriteLine("This is not a navigation user control");Ā Ā Ā Ā Ā Ā item.Visible = false;Ā Ā }}
+ 1
Can give more information ? What are you trying to do ?
+ 1
Edited my question.
0
Thanks for the answer. That is much simpler, and that foreach is probably faster than a for-loop and if-statement.