EntityCollection cast exceptions

Posts   
 
    
sbense
User
Posts: 55
Joined: 24-Jul-2007
# Posted on: 07-Nov-2007 14:33:31   

Hi,

Sorry if this is obvious but I can't figure this out.

I am using the two classes generated code and have an entity object declared like this in a TblRevisionEntity.vb

Public Class TblRevisionEntity Inherits EntityBase2
'Code not shown
End Class

Else where I have a property which refers to the collection of these entities Public ReadOnly Property Revisions() As EntityCollection(Of TblRevisionEntity) Get Return (Me.mRevisions) End Get End Property

My question is why does this cast of Revisions to either one of these collections not work?


Dim collectionA As EntityCollectionBase2(Of EntityBase2)
Dim collectionB As EntityCollection(Of EntityBase2)

collectionA = Revisions
collectionB = Revisions
'OR
collectionA = CType(Revisions,EntityCollectionBase2(Of EntityBase2))
collectionB = CType(Revisions,EntityCollection(Of EntityBase2)

Both the above fail, with or without casting using ctype. If TblRevisionEntity inherits from EntityBase2 why won't the collection cast?

Thank you for your help here.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Nov-2007 14:54:26   

If TblRevisionEntity inherits from EntityBase2 why won't the collection cast?

Having TblRevisionEntity Inherits EntityBase2 Doesn't mean that EntityCollection(Of TblRevisionEntity) inherits from EntityCollection(Of EntityBase2)

stefcl
User
Posts: 210
Joined: 23-Jun-2007
# Posted on: 07-Nov-2007 20:36:54   

Here's a nice article that covers generic variance. http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx

sbense
User
Posts: 55
Joined: 24-Jul-2007
# Posted on: 07-Nov-2007 22:14:19   

Walaa wrote:

If TblRevisionEntity inherits from EntityBase2 why won't the collection cast?

Having TblRevisionEntity Inherits EntityBase2 Doesn't mean that EntityCollection(Of TblRevisionEntity) inherits from EntityCollection(Of EntityBase2)

Thanks for the prompt response. I understand your point.

The thing is sometimes you need to refer to the entity object collections so you can access the fields of the underlying data that it represents and other times you just need an interface reference. It is not always obvious what collections should be used in different scenarios.

Practise makes perfect I suppose. This forum is simply invaluable. Thanks for all the excellent support provided.

sbense
User
Posts: 55
Joined: 24-Jul-2007
# Posted on: 07-Nov-2007 22:15:06   

stefcl wrote:

Here's a nice article that covers generic variance. http://blogs.msdn.com/rmbyers/archive/2005/02/16/375079.aspx

Thank you very much for your contribution. Useful link.