   [.net] Question about returning a TYPE from a method
 Private namespaceName As String = "System.Web.UI.WebControls." Private asmblyName As String = ", System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Public Function GetAllControls(ByVal ctrls As ControlCollection) As List(Of Control)   Dim retCtrls As List(Of Control) = New List(Of Control)   For Each ctl As Control In ctrls  retCtrls.Add(ctl)  Dim subCtrls As List(Of Control) = GetAllControls(ctl.Controls)  retCtrls.AddRange(subCtrls)   Next   Return retCtrls    End Function    Public Function GetTypeOfControls(ByVal controlType As String, ByVal parentControl As ControlCollection) As List(Of Control)   Dim allCtrls As List(Of Control) = GetAllControls(parentControl)   Dim retCtrls As List(Of Control) = New List(Of Control)   Dim neededType As Type = Type.GetType(namespaceName & controlType & asmblyName, True, True)   For Each neededCtrl As Control In allCtrls  If (neededCtrl.GetType().Equals(neededType)) Then Dim addCtrl = Convert.ChangeType(neededCtrl, neededType) retCtrls.Add(addCtrl)  End If   Next   Return retCtrls    End FunctionFor the GetTypeOfControls is there a way I can make return a different type (ex: List (Of Button), List (Of CheckBox), or List (Of Label)) on the fly? Or do I have to explicitly make a method to return the different types?edit: this is with .NET 3.5[Edited by - Alpha_ProgDes on June 2, 2010 12:12:49 PM]
  This is possible in C# 4.0 with covariance and contravariance. I don't use VB.NET, but I would assume they would have added the same.   ;  If vb.net supports generic methods you could use them to filer the list to only that object, if not have the method accept a Type object (typeOf()?) for filtering.Sorry if this isn't very helpful but I also mostly use C#.   ;  In C# 2.0 you could do something like this:public List<T> GetTypeOfControls<T>(ControlCollection parentControl) where T : Control{    List<Control> allCtrls = GetAllControls(parentControl);    List<T> retCtrls = new List<T>();    foreach (Control neededCtrl in allCtrls)   if (neededCtrl is T)  retCtrls.Add(neededCtrl);    return retCtrls;}If VB.Net has generics and an 'is' equivalent then you could do the same.In C# 3.0+ you could do this:return GetAllControls(parentControl).Where(x => x is T).ToList();;  The OfType<T>() linq method does the same thing as the select Nypyren posted, but is extra awesome because it turns an untyped IEnumerable into a typed IEnumerable.   