473,383 Members | 1,862 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,383 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 1546
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.