Вы можете использовать метод LINQ .ToArray()
. Или используйте свойство Item[Int32]
.
Dim s As ObservableCollection(Of String)
' Whatever code to fill that collection.
' First solution:
Dim arr1 = s.ToArray() ' This needs the System.Linq namespace to be imported.
' Second solution:
Dim arr2(s.Count - 1) As String
For i As Integer = 0 To s.Count - 1
arr2(i) = s.Item(i)
Next
А вот полный фрагмент рабочего кода:
Imports System
Imports System.Collections.ObjectModel
Imports System.Linq
Public Module Module1
Public Sub Main()
Dim s As New ObservableCollection(Of String)()
s.Add("Hello")
s.Add("World")
' Whatever code to fill that collection.
' First solution:
Dim arr1 = s.ToArray() ' This needs the System.Linq namespace to be imported.
Console.WriteLine("First array:")
Console.WriteLine(String.Join(", ", arr1))
Console.WriteLine()
' Second solution:
Dim arr2(s.Count - 1) As String
For i As Integer = 0 To s.Count - 1
arr2(i) = s.Item(i)
Next
Console.WriteLine("Second array:")
Console.WriteLine(String.Join(", ", arr2))
End Sub
End Module
Вывод:
First array:
Hello, World
Second array:
Hello, World
Или, если вы хотите использовать для каждого l oop, вы можете сделать это так:
Dim arr2(s.Count - 1) As String
Dim i As Integer = 0
For Each str In s
arr2(i) = str
i += 1
Next