Convert Array to Generic List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Kennedy

    Convert Array to Generic List

    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

    #2
    Re: Convert Array to Generic List

    On Aug 31, 8:25 pm, "Chris Kennedy" <chris.kenned.. .@btinternet.co m>
    wrote:
    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.Collecti ons.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.AddRang e(intArray)



    Hope this helps,

    Onur Güzel

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: Convert Array to Generic List

      Chris Kennedy wrote:
      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)(stringA rray)
      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.FindAl l(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.FindAl l(AddressOf FindId)

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      Working...