M
Mythran
Since there is no (or I haven't found one) an equivalent in VB.Net to C#'s
"as" operator, I decided to try writing a generic method to perform a
similar operation....any thoughts or suggestions on what I have thus far?
Private Function [As](Of T)(ByVal instance As Object) As T
If TypeOf instance Is T
Return DirectCast(instance, T)
End If
Return Nothing
End Function
Any thoughts or suggestions?
use would be:
Dim b As Control = New Button()
Dim a As Button = [As](Of Button)(b)
If a IsNot Nothing
' b is a control that is a button and not null.
End If
What d'ya think? The alternative to using this method would be:
Dim b As Control = New Button()
Dim a As Button
If TypeOf b Is Button
a = DirectCast(b, Button)
End If
If b IsNot Nothing
' b is a control that is a button and not null.
End If
???
Note: Code above may not compile...I wrote it off the top of my head and
didn't perform testing on it...yet.
Thanks,
Mythran
"as" operator, I decided to try writing a generic method to perform a
similar operation....any thoughts or suggestions on what I have thus far?
Private Function [As](Of T)(ByVal instance As Object) As T
If TypeOf instance Is T
Return DirectCast(instance, T)
End If
Return Nothing
End Function
Any thoughts or suggestions?
use would be:
Dim b As Control = New Button()
Dim a As Button = [As](Of Button)(b)
If a IsNot Nothing
' b is a control that is a button and not null.
End If
What d'ya think? The alternative to using this method would be:
Dim b As Control = New Button()
Dim a As Button
If TypeOf b Is Button
a = DirectCast(b, Button)
End If
If b IsNot Nothing
' b is a control that is a button and not null.
End If
???
Note: Code above may not compile...I wrote it off the top of my head and
didn't perform testing on it...yet.
Thanks,
Mythran