Solved

Need help with "Unable to cast object" error

Posted on 2013-12-17
2
730 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
2 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now