How to Make Function Return Generic Value in VB.NET
I have an abstract class in VB.NET. I want all classes that inherit from this class to return whatever value makes sense. For exam开发者_开发技巧ple, it could be an Decimal, Integer, String. How can I delcare the function in the abstract base class to allow for this? Is this even possible?
Right now, my abstract class looks like this:
Public MustInherit Class ConversionBaseClass
Public MustOverride Function GetConvertedObject()
End Class
I know its return value needs modified, but im not sure as to what. Something like this maybe?
Public MustOverride Function GetConvertedObject() Of (T)
Public MustOverride Function GetConvertedObject(Of T)() As T
should do it.
Make the Base class be generic of T then you can use it for the method
Public MustInherit Class ConversionBaseClass (Of T)
Public MustOverride Function GetConvertedObject() As T
End Class
Then you define your inheriting classes like
public Class IntConversion Inherits ConversionBaseClass(Of Integer)
Public Overrides Function GetConvertedObject() As Integer
精彩评论