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)?
开发者_如何学GoThe 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
I just wanted to mention (in case anyone is wondering something similar) that, while SLaks is right regarding the arithmetic, I read about a neat way to restrict the generic to comparable value types:
(Of T As {Structure, IComparable})
Unfortunately, this is not possible.
There are two workarounds.
精彩评论