Cannot Convert Type of LinkedList(Of X) to LinkedList(Of Y)

  • Thread starter Thread starter Phillip.Ross.Taylor
  • Start date Start date
P

Phillip.Ross.Taylor

When I designed my application I created an object called "Orderable"
which exposes a public property "sequence".

Then a few objects inherit from this. I'll just call them ObjectX for
the sake of it. Then I wrote a method called public sub
MySortProcedure (byref list as LinkedList(Of Orderable))

but I cannot pass in my list of objects derived from it which were
originally declared like this:

dim mySelectedObjects as LinkedList(Of ObjectX)

How can I get around this scenario? I don't want to copy the
linkedlist everytime and I don't want to bind MySortProcedure to a
LinkedList(Of ObjectX) so what should I do?
 
When I designed my application I created an object called "Orderable"
which exposes a public property "sequence".

Then a few objects inherit from this. I'll just call them ObjectX for
the sake of it. Then I wrote a method called public sub
MySortProcedure (byref list as LinkedList(Of Orderable))

but I cannot pass in my list of objects derived from it which were
originally declared like this:

dim mySelectedObjects as LinkedList(Of ObjectX)

How can I get around this scenario? I don't want to copy the
linkedlist everytime and I don't want to bind MySortProcedure to a
LinkedList(Of ObjectX) so what should I do?

I can't wait for a reply so basically I have to have per object
specific functions involved now like this:

public sub MySortProcedure (byref list as LinkedList(Of Orderable))
'blah
end sub

Public Function convertObjectX(byref list as LinkedList(Of ObjectX))
as LinkedList(Of Orderable)

Dim ll As New LinkedList(Of Orderable)

For Each i As ObjectX In list
ll.AddLast(i)
Next

Return ll
End Function

This works but I have to write a few of these convertObject functions.
Is this really the only way to do this? I'm sure other languages can
cope with generics properly or am I mistaken?

Phill
 
When I designed my application I created an object called "Orderable"
which exposes a public property "sequence".

Then a few objects inherit from this. I'll just call them ObjectX for
the sake of it. Then I wrote a method called public sub
MySortProcedure (byref list as LinkedList(Of Orderable))

but I cannot pass in my list of objects derived from it which were
originally declared like this:

dim mySelectedObjects as LinkedList(Of ObjectX)

How can I get around this scenario? I don't want to copy the
linkedlist everytime and I don't want to bind MySortProcedure to a
LinkedList(Of ObjectX) so what should I do?

You should already be able to add ObjectX items to a LinkedList(Of Orderable)
since ObjectX inherits from Orderable.

Alternatively (and perhaps better)..
....Create an Interface "IOrderable"

Interface IOrderable
Property Sequence as Integer ' <- Just a guess
End Interface

Then implement this interface in Orderable

(SideNote: 'Orderable' is perhaps not the best name (for a class anyway)
as generally <verb>able implies an interface. Class names are better named
after Nouns...Perhaps "Product" or "Order"....
This is just my opinion but I have seen many people follow this sort of strategy
uin naming their Classes/Interfaces and therefore this might make it easier
for others to understand your code. However this sis just a suggestion and
since I know next to nothing about your project, I may have gotten this completely
wrong and you may have a great reason for doing what you have. In which case
just ignore this bit :) Anyway... I digress....)

Now you can add any object which implement IOrderable (Like Orderable Objects
and ObjectX Objects and later anything else you need) to a LinkedList(Of
IOrderable).

All Objects will pop out as IOrderable objects and will let you access the
Sequence Property

I hope this helps
 
You should already be able to add ObjectX items to a LinkedList(Of Orderable)
since ObjectX inherits from Orderable.

Alternatively (and perhaps better)..
...Create an Interface "IOrderable"

Interface IOrderable
Property Sequence as Integer ' <- Just a guess
End Interface

Then implement this interface in Orderable

(SideNote: 'Orderable' is perhaps not the best name (for a class anyway)
as generally <verb>able implies an interface. Class names are better named
after Nouns...Perhaps "Product" or "Order"....
This is just my opinion but I have seen many people follow this sort of strategy
uin naming their Classes/Interfaces and therefore this might make it easier
for others to understand your code. However this sis just a suggestion and
since I know next to nothing about your project, I may have gotten this completely
wrong and you may have a great reason for doing what you have. In which case
just ignore this bit :) Anyway... I digress....)

Now you can add any object which implement IOrderable (Like Orderable Objects
and ObjectX Objects and later anything else you need) to a LinkedList(Of
IOrderable).

All Objects will pop out as IOrderable objects and will let you access the
Sequence Property

I hope this helps

Thanks for your reply Rory.

I understand what your saying about putting individual objects or
types directly into the more generic version of the array from the
start but it's not really appropriate for me. Most of the operations
are on the higher operations anyway and the linked list are guaranteed
to be of the same, subclassed type. If I'm being brutally honest the
different objects are all completely different, it's just they can be
sequenced and I thought I could hit a couple of nails with one big
generic hammer. Existing code and the white space overhead of casting
every object everytime for 90% of the existing operations on those
linked lists really isn't appropriate for this.

I've also tried using Interfaces, as you suggested, instead and it
gives me exactly the same result / error message. It just won't
compile.

As for the naming, I know exactly what you mean. I became Orderable
after I made an interface out of it earlier. Unfortunetly I've used
the name Sortable somewhere else and it means something else and
UserResequencable just sounded wrong.

I'm currently steaming down a path where I have a page of functions,
each, to take a linked list of ObjectX and downcast it to a linked
list of Orderable. [wow, it does sound bad when you say it out loud].

Anyway thanks for your comments Rory, if you or anyone else has any
other more appropriate solutions I'd like to hear them.

Thanks,

Phill
 
Thanks for your reply Rory.
...
As for the naming, I know exactly what you mean. I became Orderable
after I made an interface out of it earlier. Unfortunetly I've used
the name Sortable somewhere else and it means something else and
UserResequencable just sounded wrong.

I'm currently steaming down a path where I have a page of functions,
each, to take a linked list of ObjectX and downcast it to a linked
list of Orderable. [wow, it does sound bad when you say it out loud].

Ok... lets look at this from another angle then.

I am unfamiliar with the dotnet 2.0 collection classes but understand the
ideas behind generics.
(We started the switch to VS2005 today :D )

It seems that your reason for wanting to downcast is your idea of writing
a common sort routine.

If this is your only reason then perhaps you could implement Icomparable
for each derivative type and add your items to a generic arraylist (does
that exist in 2.0?)

If I were trying to do this with 1.1 classes, I would use IComparable and
Icompare to do my sorting and perhaps store my items in an Arraylist.

Does this put a different spin on things?
 
How can I get around this scenario? I don't want to copy the
linkedlist everytime and I don't want to bind MySortProcedure to a
LinkedList(Of ObjectX) so what should I do?

Make the method generic as well

Sub MySortProcedure(Of T As Orderable)(ByVal list As LinkedList(Of T))



Mattias
 
Make the method generic as well

Sub MySortProcedure(Of T As Orderable)(ByVal list As LinkedList(Of T))

Mattias

I tip my hat to you Mattias, you are indeed an expert. That was
exactly what I was looking for. I didn't even know VB.NET was capable
of this power. Thank you.

And thanks Rory for your input. I was going to implement your
IComparable idea before I read Mattias's post.

Thanks guys

Phill
 
Back
Top