I’m currently working on a VB.Net generic function that eats two structures and returns a structure, like so…
Public Function MyFunction (Of ThisType As {Structure, MyInterface}) _
(ByVal first_param As ThisType, _
ByVal second_param As ThisType, _
ByVal third_param As Object, _
ByVal fourth_param As Object) As ThisType
' Do works
End Function
but the function will fail at run-time unless the third_parameter and fourth_parameter are also structures that implement MyInterface. Is there any way to constrain the third and fourth parameters to be structures of such a type (there are numerous possibilities for the structures; too many to create overloads for each possibility), so that an error will show up at compile time?
(I tried deriving all the structures from a base structure but as I now know one can’t do that.) I can check the types before using them but that is still a run-time check.
You can give a generic function more than one type. Simply add a second Type parameter and then constrain it to the types you want the same as the first.