473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 mySelectedObjec ts 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 1567
On 14 Jun, 10:23, Phillip.Ross.Ta y...@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 mySelectedObjec ts 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 mySelectedObjec ts 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...@new sgroup.nospamwr ote:
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 mySelectedObjec ts 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
UserResequencab le 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...@new sgroup.nospamwr ote:
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
UserResequencab le 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(O f ObjectX) so what should I do?
Make the method generic as well

Sub MySortProcedure (Of T As Orderable)(ByVa l 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.w ant.s...@mvps.o rg>
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)(ByVa l list As LinkedList(Of T))

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/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
13978
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. Here's the code: protected void serverValidateCboBx(object source, ServerValidateEventArgs args)
6
14567
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 server. I am able to log in. However after publishing the page to my local IIS, it results in the below error. This error is occurring on the Visual Studio.Net 2k5 release version, but did not occur on the beta 2, same code. A point of interest, is...
2
14808
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 webBrowser1_DocumentCompleted method I have the following code. mshtml.HTMLDocument oDoc = new HTMLDocumentClass(); oDoc = (mshtml.HTMLDocument)webBrowser1.Document;
2
5016
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 'System.Web.UI.WebControls.Login' Anyone have an idea about the cause? Thanks,
2
17174
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 compare both? Example:
6
30194
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
8198
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 accessing to the Generic class. private double mtrxgraphdata; private XMLdocument Matrixdata;
2
10286
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 2005\Projects\CodBarraPalm\CodBarraPalm\Default.aspx.cs 35 26 CodBarraPalm ? why this error trying to get the contents of a row on a DataSet?
2
4675
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 of error before and found a way around it, it would be greatly appreciated. error CS0030: Cannot convert type
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.