Aaron Palmer
Aaron Palmer

Reputation: 9022

How to convert List(Of String) to Object()?

I know what you're thinking, why do you want to convert in that direction? Basically, I have to make modern code palatable for a legacy COM+ app.I've seen a lot of conversion ideas going the other direction.

The only thing I've come up with it actually looping through the List(Of String). Perhaps this is the best/only way.

This works, but seems cludgey.
groupNames is a List(Of String)

Dim groups() As Object = New Object() {}

If groupNames IsNot Nothing Then
    groups = New Object(groupNames.Count - 1) {}
    For i = 0 To groupNames.Count - 1
        groups(i) = groupNames(i)
    Next
End If

Upvotes: 1

Views: 80

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

It's a little cleaner if you use the LINQ Cast extension method:

Dim strings As New List(Of String)({"1", "2"})
Dim objects() As Object = strings.Cast(Of Object).ToArray()

Upvotes: 1

danish
danish

Reputation: 5600

You can use good old array here. There is no need to lose strong typed behaviour.

Upvotes: 0

Related Questions