Solved

Need help with "Unable to cast object" error

Posted on 2013-12-17
2
769 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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