VB.NET, .NET 4
Hello all,
Suppose I have an interface called IParseable(Of TParsed, TUnparsed) which requires two functions:
Function Parse(ByVal Value As TUnparsed) As TParsed
Function Unparse(ByVal Value As TParsed) As TUnparsed
Is there a way that I can restrict TParsed and TUnparsed to be numeric types (for which operations like “*” and “+” are already defined)?
The problem is that, when I try to implement my interface and define one of the functions, e.g.:
Public Function Parse(ByVal Value As TUnparsed) As TParsed Implements IParseable(Of TParsed, TUnparsed).Parse
Return CType(10 * Value, TParsed)
End Function
VS throws an error saying the “*” is not defined for TUnparsed. I understand that, since TUnparsed could be anything, but is there a way to restrict my generic such that, say, TUnparsed could only be Double, Integer, Long, etc?
I ask this because I know that you can do something like:
Function Blah(Of T As TextBox)(ByVal Control As T) As Object
To require Control to be a TextBox (or maybe I don’t understand that very well either….). But, anyway, any idea or am I way off track? Just trying to get a hang of these interface thingies and generic types.
Thanks a lot in advance,
Brian
Unfortunately, this is not possible.
There are two workarounds.