user489041
user489041

Reputation: 28294

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 example, 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)

Upvotes: 0

Views: 1609

Answers (2)

Bob Vale
Bob Vale

Reputation: 18474

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

Upvotes: 2

Blindy
Blindy

Reputation: 67380

Public MustOverride Function GetConvertedObject(Of T)() As T

should do it.

Upvotes: 5

Related Questions