+ 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));

31st Jan 2018, 12:49 AM
Brendon B
Brendon B - avatar
5 Respostas
+ 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;   }}
31st Jan 2018, 10:13 PM
sneeze
sneeze - avatar
31st Jan 2018, 10:15 PM
sneeze
sneeze - avatar
+ 1
Can give more information ? What are you trying to do ?
31st Jan 2018, 10:59 AM
sneeze
sneeze - avatar
+ 1
Edited my question.
31st Jan 2018, 9:02 PM
Brendon B
Brendon B - avatar
0
Thanks for the answer. That is much simpler, and that foreach is probably faster than a for-loop and if-statement.
1st Feb 2018, 8:54 AM
Brendon B
Brendon B - avatar