0

在下面的代码中,函数 CreatePage 有一个名为 thumbs 的参数。thumbs 是一个数组列表。我的问题是,您是否知道是否可以将 arraylist thumbs 拆分为 15 的部分并使用 thumbs(1-15)、thumbs(16-30)、thumbs(31-45) 等调用该函数。直到arraylist为空。

html.CreatePage(txtTitleTag.Text, txtText.Text, "index", txtDirectory.Text & "\", thumbs,  txtMetaDesc.Text, txtMetaKeywords.Text, "test.com", "test2.com",  BackgroundColor, FontColor)
4

1 回答 1

1

首先,从 a 切换ArrayList到 a List(Of String)ArrayList在 .NET 2.0 中已经过时了,您将从强类型中受益匪浅。

接下来,这是一个分块 a 的方法List

<System.Runtime.CompilerServices.Extension()>
Public Function Chunk(Of T)(ByVal this As List(Of T), ByVal length As Integer) As List(Of List(Of T))
    Dim result As New List(Of List(Of T))
    Dim current As New List(Of T)

    For Each x As T In this
        current.Add(x)

        If current.Count = length Then
            result.Add(current)
            current = New List(Of T)
        End If
    Next

    If current.Count > 0 Then result.Add(current)

    Return result
End Function

现在,只需使用For Each遍历块的循环:

For Each chunk As List(Of String) In myList.Chunk(15)
    'Call the function and pass chunk as an argument
Next

瞧!

于 2012-04-29T15:37:13.590 回答