Connecting Tech Pros Worldwide Help | Site Map

Convert Array to Generic List

Chris Kennedy
Guest
 
Posts: n/a
#1: Aug 31 '08
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.

kimiraikkonen
Guest
 
Posts: n/a
#2: Aug 31 '08

re: Convert Array to Generic List


On Aug 31, 8:25*pm, "Chris Kennedy" <chris.kenned...@btinternet.com>
wrote:
Quote:
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
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#3: Sep 1 '08

re: Convert Array to Generic List


Chris Kennedy wrote:
Quote:
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)
Quote:
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)

--
Göran Andersson
_____
http://www.guffa.com
Closed Thread


Similar Visual Basic .NET bytes