Convert Array to Generic List

C

Chris Kennedy

I am getting a generic list of objects from a webservice. The list is
converted to an array by this process. How do I convert the array back to a
list and is there any way of finding an object within the list via one it's
properties. I have seen it done in C# but not VB.
E.g. obj = get object from list where obj.id = 1. I could do some kind of
solution using loops but I was hoping for something a little more slick.
This is for .net 2.0 by the way. Regards, Chris.
 
K

kimiraikkonen

I am getting a generic list of objects from a webservice. The list is
converted to an array by this process. How do I convert the array back toa
list and is there any way of finding an object within the list via one it's
properties. I have seen it done in C# but not VB.
E.g. obj = get object from list where obj.id = 1. I could do some kind of
solution using loops but I was hoping for something a little more slick.
This is for .net 2.0 by the way. Regards, Chris.

Hi,
I don't know if it doesn't what you're looking for, but List type has
AddRange method to adds elements into List.


Imports System
Imports System.Collections.Generic
' Assuming you have an Integer array of IDs
Dim intArray() As Integer = {0,1,2,3....} ' goes on
Dim intList As New List(Of Integer)
intList.AddRange(intArray)



Hope this helps,

Onur Güzel
 
G

Göran Andersson

Chris said:
I am getting a generic list of objects from a webservice. The list is
converted to an array by this process. How do I convert the array back
to a list

You just create a list and supply the array to the constructor. It will
then loop through the array and add them to the list. Example:

Dim stringList As New List(Of String)(stringArray)
and is there any way of finding an object within the list via
one it's properties. I have seen it done in C# but not VB.
E.g. obj = get object from list where obj.id = 1. I could do some kind
of solution using loops but I was hoping for something a little more
slick. This is for .net 2.0 by the way. Regards, Chris.

If you have VS2008, you can use anonymous methods in VB. If I remember
the syntax correctly:

Dim result As List(Of SomeClass) = someList.FindAll(Function(item As
SomeClass) item.id = 1)

Otherwise you have to use a named method:

Function FindId(item As SomeClass)
Return item.id = 1
End Function

Dim result As List(Of SomeClass) = someList.FindAll(AddressOf FindId)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top