On 14 Jun, 16:15, Rory Becker <RoryBec...@newsgroup.nospamwrote:
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
--
Rory
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