1

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
2
  • 1
    Not sure this is what you're looking for groupNames.Cast<Object>().ToArray() Commented Dec 26, 2013 at 15:48
  • 1
    Yes it is, and yes I feel dumb Commented Dec 26, 2013 at 15:52

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I still have to do a null check on "strings" because it's passed in as a parameter to my method... but this is what I was looking for. Thanks.
0

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

7 Comments

You're talking about covariance I think, that is broken, will fail when try to set a value.
I am referring to string array.
but op is asking for object[]
I do need object[] because the COM component throws a type mismatch when it gets a string[]
VB6 typing is different than .net.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.