Last post Aug 27, 2008 11:13 AM by rasor
Sep 05, 2007 11:13 AM|common01|LINK
Hi, I can't get this simple example working, what's the problem ?
Public Interface IPage
ReadOnly Property ID() As Integer
Public Class tst
Private val As Integer
Public ReadOnly Property ID() As Integer Implements IPage.ID
Partial Class Admin_Menu_Default
Public Sub SomeSub()
Dim pagelist As List(Of IPage)
pagelist = New List(Of tst)
Error 5: Value of type 'System.Collections.Generic.List(Of Tisno.Data.tst)' cannot be converted to 'System.Collections.Generic.List(Of Tisno.Data.IPage)'.
Can someone please help me ?
Sep 05, 2007 11:43 AM|ps2goat|LINK
You already declared the 'pagelist' variable as a list of IPage-- you can't all of a sudden narrow it to a more specific IPage implementing class!
You can, however, add any tst object to the pagelist object. The only difference is that your tst objects will now allow only calls to IPage properties and methods. To call a tst class-specific property or method, you would have to cast the IPage object
back to a tst object, and then make the call.
Dim pagelist As New List(Of IPage)
Dim _tst1 As New tst()
Dim _tst2 As New tst()
Aug 27, 2008 11:13 AM|rasor|LINK
I was annoyed about it too.
Apparently also some Microsoft people was, so at least they made a convertion function you can call, ConvertAll(), but you have to implement a convert function yourself. See this post:
The code would be more readable, if you just looped the list and added the elements inside to a list of the other type, but at least a pattern is nice.
Visual Basic .NET