Solved

Need help with "Unable to cast object" error

Posted on 2013-12-17
2
784 Views
Last Modified: 2016-02-10
I am trying to create an arraylist of unique elements from two arraylists using Vb.net & linq but I get the following error on the last line of the embedded code.

"Unable to cast object of type '<DistinctIterator>d__81`1[System.Object]' to type 'System.Collections.ArrayList'."

this is my code:
Dim arr1 as arraylist=some mixture of integers and strings      
Dim arr2 as arraylist=some mixture of integers and strings    
Dim arr As New ArrayList
Dim DistinctArray As New ArrayList
Dim pos As Integer
Dim str As String

arr.AddRange(arr1)
arr.AddRange(arr2)
DistinctArray = (From s In arr Select s).Distinct

Open in new window

I am not really that familiar with linq so I'd love any help.
0
Comment
Question by:NevSoFly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39726241
Your problem is that the results from the Linq clause is not an ArrayList, so it cannot cast right.

Try this:
Dim arr1 as arraylist=some mixture of integers and strings      
Dim arr2 as arraylist=some mixture of integers and strings    
Dim arr As New ArrayList

arr.AddRange(arr1)
arr.AddRange(arr2)
Dim DistinctArray = (From s In arr Select s).Distinct

Open in new window


From this point, you can manage DistinctArray in the same way as it was an ArrayList. But don't declare it explicitly as ArrayList or you'll get the error.

Hope that helps.
0
 

Author Closing Comment

by:NevSoFly
ID: 39726378
Thanks, your code worked.  After playing around with it I did manage to get the arraylist result I originally wanted.  Maybe this will help someone else out.

Dim arr1 as arraylist=some mixture of integers and strings      
Dim arr2 as arraylist=some mixture of integers and strings  
Dim DistinctArray As New ArrayList  
Dim arr As New ArrayList

arr.AddRange(arr1)
arr.AddRange(arr2)
DistinctArray.AddRange((From s In arr Select s).Distinct.toList)
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question