Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with "Unable to cast object" error

Posted on 2013-12-17
2
Medium Priority
?
815 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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