Hi All,
I have created a couple of simple functions in VB.NET that simply return a control of a certain type that I already know, such as HtmlInputHidden, Label, etc. That means that each of the functions is created only for that special purpose.
What I'd like to do is combine all those functions into one function using generics. The common things shared by each of the functions is a control Id and a control type.
What I have got so far is:
Public Function GetControl(Of T)(ByVal ctrlId As String) As T
Dim ctrl As Control = Me.FindControl(ctrlId)
If (Not ctrl Is Nothing) Then
GetControl = CType(ctrl, T)
Else
GetControl = Nothing
End If
End Function
But the line " GetControl = CType(ctrl, T)" is giving me a compile error:
Value of type 'System.Web.UI.Control' cannot be converted to 'T'
This is in .NET Framework 2.0.
Any insight is highly appreciated.
John