在循环中设置数组的第二个维度

时间:2015-06-19 14:19:01

标签: vb.net

我想根据for循环中的第一个维度进行dinamycically设置第二个数组维度。我现在无法找到任何相关文档。如果有人拥有它,我不介意被引用到正确的地方。

Dim array(3,0) As String
For y As Integer = 0 To array.GetUpperBound(0) - 1

            <<Set here the second dimension of the array to another dynamic variable 
            ( Something Like array(y).dimensionsize = X. Where y is my first dimension argument)>>

            For i As Integer = 0 To array.GetUpperBound(1) - 1
                array(y, i) = "something"
            Next
        Next

这可能吗?

编辑:

情况是我从AD获得了团体成员资格。用户通常有多个帐户。

第一个循环遍历帐户数量,第二个循环遍历每个帐户中的广告组。

UserResult是AD查询结果

这是在开始时的声明,因为它通过背景工作者。我需要它是全球性的:

 Public Shared UserInfoGroup(1)() As String

我对我的backgroundworker.dowork感兴趣的部分:

 ReDim UserInfoGroup(UserResult.Count)(1)

        For y As Integer = 0 To UserResult.Count - 1

            Dim UserGroupArray As PrincipalSearchResult(Of Principal) = UserResult(y).GetGroups()
            ReDim UserInfoGroup(UserResult.Count)(UserGroupArray.Count)
            For i As Integer = 0 To UserGroupArray.Count - 1
                UserInfoGroup(y)(i) = UserGroupArray(i).ToString()
            Next
        Next

backgroundworker.completed然后将数组输出到表单。

2 个答案:

答案 0 :(得分:3)

如果每个“y”具有不同数量的“x”而不是需要锯齿状数组,而不是2D数组。

Dim array(3)() As String

For y As Integer = 0 To array.GetUpperBound(0) - 1

    ReDim array(y)(x)
    ' or
    ' array(y) = New String(x) {}

    For i As Integer = 0 To x
        array(y)(i) = "something"
    Next
Next

更好的选择可能是拥有一个列表数组。

Dim array(3) As List(Of String)    

For y As Integer = 0 To array.GetUpperBound(0) - 1

    array(y) = New List(Of String)

    For i As Integer = 0 To x
        array(y).Add("something")
    Next
Next

答案 1 :(得分:0)

您可以使用:

Redim Preserve array(3, x)