Home All Groups Group Topic Archive Search About

Convert Array of Objects to Generics List?

Author
16 Jul 2006 5:40 AM
Spam Catcher
For example, I have the following:

Dim MyObjectArray() as Object = External.Function()

Is it possible to convert MyObjectArray into a Generics Collection.List?

Thanks.

Author
16 Jul 2006 2:51 PM
Jay B. Harlow [MVP - Outlook]
Spam,
Have you tried:

| Dim MyObjectArray() as Object = External.Function()
    Dim theList As New List(Of Object)(MyObjectArray)

If you really want theList to be List(Of Whatever) then you need to start
with an array of Whatever or convert the MyObjectArray to an array of
Whatever first.


| Dim MyObjectArray() as Object = External.Function()
        Dim MyWhateverArray() As Whatever
        MyWhateverArray = Array.ConvertAll(Of Object,
Whatever)(MyObjectArray, AddressOf Converter)
        Dim theList As New List(Of Whatever)(MyWhateverArray)

    Private Function Converter(ByVal value As Object) As Whatever
        Return CType(value, Whatever)
    End Function

Alternatively you could use a For Each to read the MyObjectArray adding the
items to theList.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message
news:Xns98021103B2928usenethoneypotrogers@127.0.0.1...
| For example, I have the following:
|
| Dim MyObjectArray() as Object = External.Function()
|
| Is it possible to convert MyObjectArray into a Generics Collection.List?
|
| Thanks.
|