473,226 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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


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?

Jun 14 '07 #1
6 1537
On 14 Jun, 10:23, Phillip.Ross.Tay...@gmail.com wrote:
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

Jun 14 '07 #2
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

Jun 14 '07 #3
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

Jun 14 '07 #4
On 14 Jun, 16:15, Rory Becker <RoryBec...@newsgroup.nospamwrote:
Thanks for your reply Rory.
...
<snip/>
...
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?

--
Rory

Jun 14 '07 #5
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 14 '07 #6
On Jun 14, 10:00 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
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

Jun 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Assimalyst | last post by:
Hi, I am trying to write some server side validator code in C#. I want to have a piece of code that can be used by all DropDownLists on a webform, giving a false value if the SelectedIndex is 0....
6
by: Tim Cartwright | last post by:
I have a page that has the login control on it, nothing else. This page inherits from a master page, neither page has any code in it. This page works perfectly when running on the WebDev debug web...
2
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the...
2
by: keithb | last post by:
A web site published from its development environment (VS Web Server) to an IIS 6.0 site displays this message: Compiler Error Message: CS0030: Cannot convert type 'ASP.login_aspx' to...
2
by: Marcelo Muzilli | last post by:
Howdy all, in my program, I have a ushort variable and a string variable and if I try to compile it, I'm receiving this error message: "Cannot convert type 'string' to 'ushort'". How can I...
6
by: John | last post by:
The following code: int test = 1; bool isTrue = (bool)test; results in a compiler error: Cannot convert type 'int' to 'bool' wtf, any ideas on how to work around this?
1
by: niharnayak2003 | last post by:
Hi All, I am facing the problem in Typecast inside the Generic class. I need a function which will take two param 1:-Xpath 2:- XMLDOC and return me what i will need. here is the code for...
2
by: Paulo | last post by:
DataRow dr = (DataRow)ds.Tables.Rows.ItemArray; Error 1 Cannot convert type 'object' to 'System.Data.DataRow' C:\Documents and Settings\Fabio\Meus documentos\Visual Studio...
2
by: nomad | last post by:
Hi, When trying to serialize a class I keep getting the message below. optionalExtensionTypeOptionalExtension is not mandatory so not sure why this is appearing. If anyone has seen this type...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.