6 Replies - 406 Views - Last Post: 21 April 2011 - 07:54 PM Rate Topic: -----

#1 l1on3l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 11

Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 02:07 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Class AAA(Of T As ITestInterface(Of T)

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 02:09 PM

Describe "Not Working".
Any error message?
Unexpected results? (What you think should have happen? What actually happened?)
Was This Post Helpful? 0
  • +
  • -

#3 l1on3l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 11

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 02:11 PM

View PostAdamSpeight2008, on 21 April 2011 - 02:09 PM, said:

Describe "Not Working".
Any error message?
Unexpected results? (What you think should have happen? What actually happened?)

Visual Studio doesn't accept it: I cannot compile my project:

Quote

Error 2 Type argument 'WindowsApplication1.TestInterfaceString' does not inherit from or implement the constraint type 'WindowsApplication1.ITestInterface(Of WindowsApplication1.TestInterfaceString)'. C:\Users\lluchez\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 32 21 WindowsApplication1

This post has been edited by l1on3l: 21 April 2011 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 03:35 PM

Hi,

The first problem is that class BBB hasn't got a contructor. You need to give it a contructor, and the call the base class constructor (AAA's constructor).

The second problem is AAA's type parameter must be of type ITestInterface(Of T), however, in your declaration of BBB, you specify TestInterfaceString as the type parameter of AAA. This breaks the constraint you set, as TestInterfaceString implements ITestInterface(Of String) NOT ITestInterface(Of T)

:)

This post has been edited by CodingSup3rnatur@l-360: 21 April 2011 - 03:38 PM

Was This Post Helpful? 0
  • +
  • -

#5 l1on3l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 11

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 03:54 PM

Thanks man for your reply.
Ok I see the problem but I still have no idea on how to solve it.
Do you have any idea how to set this BBB class a child of AAA with a fixed type (String,Integer).

Maybe I'm not doing it well and there is a better way to have this genericity while inheriting.

PS: I know for the missing New() but it was just not to make it more complex that it is. I'll handle it later on.

Any hint will be really appreciated as I'm stuck on this since 2 days.
Thanks for your help.
/Lionel
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 05:59 PM

Here's one way solving the issue.
Public Class AAA(Of T As ITestInterface(Of u), u)
  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, String)
  Public Sub New(ByVal t As TestInterfaceString)
    MyBase.new(t)
  End Sub
End Class



Was This Post Helpful? 0
  • +
  • -

#7 l1on3l  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-April 11

Re: Class AAA(Of T As ITestInterface(Of T)

Posted 21 April 2011 - 07:54 PM

View PostAdamSpeight2008, on 21 April 2011 - 05:59 PM, said:

Public Class AAA(Of T As ITestInterface(Of u), u)

This is what I was missing. I didn't know we have to declare it this way.
I was trying something like (without ", u"):
Public Class AAA(Of T As ITestInterface(Of U))
.

Thanks a lot !!! I should now be able to go on.
/Lionel
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1