Class AAA(Of T As ITestInterface(Of T)

l1on3l

Member
Joined
Apr 21, 2011
Messages
6
Programming Experience
5-10
Hi everyone,


I'm stuck on a programming problem using Interfaces and inheritance in Vb.Net. Here is what I want to do:

I got objects TestInterfaceString, TestInterfaceInt implementing an interface ITestInterface(Of T) declaring this function: Function MyTest() As T. Nothing complicated so far.

Then I want to have a generic class handling T object where T should implement the previous interface: ITestInterface(Of T). This is working too.

Finally I want to create a class that inherits from this generic last class but then I'm fixing the type (String, Integer, etc...). This is where I'm stuck...

Below the sample code:


Public Interface ITestInterface(Of T)
Function MyTest() As T
End Interface

Public Class TestInterfaceString
Implements ITestInterface(Of String)

Public Function MyTest() As String Implements ITestInterface(Of String).MyTest
Return "Hello world"
End Function
End Class

Public Class AAA(Of T As ITestInterface(Of T))
Public Sub New(ByVal t As T)
Console.WriteLine(t.MyTest().ToString())
End Sub
End Class

Public Class BBB ' not working
Inherits AAA(Of TestInterfaceString) ' not working

End Class


Thanks very much for any help...

Best regards
/Lionel Luchez
 
First the problem,
VB.NET:
Public Class AAA(Of T As ITestInterface(Of T))

Public Class BBB : Inherits AAA(Of TestInterfaceString)
compile error said:
Type argument 'TestInterfaceString' does not inherit from or implement the constraint type 'ITestInterface(Of TestInterfaceString)'.
From the error message you can see how this T is biting its own tail. For your BBB to work like that your AAA would be declared:
VB.NET:
Public Class AAA(Of T As ITestInterface(Of String))
Because this is how the type you supply to BBB is constrained.
With this AAA definition the TestInterfaceString class can be made generic, for example TestInterface(Of T). BBB would then go like this:
VB.NET:
Public Class BBB
    Inherits AAA(Of TestInterface(Of String))

If you need AAA to be fully generic you must supply both type parameters, for example:
VB.NET:
Public Class TestInterface(Of T)
    Implements ITestInterface(Of T)

Public Class AAA(Of T1, T2 As ITestInterface(Of T1))

Public Class BBB
    Inherits AAA(Of String, TestInterface(Of String))
or a less generic version similar to your example
VB.NET:
Public Class TestInterfaceString
    Implements ITestInterface(Of String)

Public Class AAA(Of T1, T2 As ITestInterface(Of T1))

Public Class BBB
    Inherits AAA(Of String, TestInterfaceString)
Notice normally you name the type parameters by purpose, for example (TKey, TValue). These examples that has no real substance can be less readable because of this. Same goes for using general meaning words like "test", keywords like "interface" and type names like "string" in the class and interface names. It is usually a lot easier to learn the theory when applied to a real world context. As I fail to see how what you're trying to do can be applied I've just used same terminology as yours in my examples, however confusing that may seem.
 
Public Class AAA(Of T1, T2 As ITestInterface(Of T1))
This is what I was missing. I was trying something like:
Public Class AAA(Of T As ITestInterface(Of U))
I just didn't know this would have work this way. Now I think I'll be able to solve my issue and go further... Do not worry about naming, this was just isolating my issue in a small project test. I usually try proper names ;)

Thanks very much John. I really appreciate it!!!
/Lio
 
Back
Top