0

In the code below there is a inparameter named thumbs to the function CreatePage. thumbs is an arraylist. My question is if you know if it's possible to split the arraylist thumbs into parts of 15 and call the function with thumbs(1-15), thumbs(16-30), thumbs(31-45) and so on. Until the arraylist is empty.

html.CreatePage(txtTitleTag.Text, txtText.Text, "index", txtDirectory.Text & "\", thumbs,  txtMetaDesc.Text, txtMetaKeywords.Text, "test.com", "test2.com",  BackgroundColor, FontColor)
2
  • What version of .NET are you using? If it's not 1.1 (I really doubt it is) then you should not be using an ArrayList. Commented Apr 29, 2012 at 14:38
  • What type of object is in the ArrayList? Strings? Commented Apr 29, 2012 at 15:25

1 Answer 1

1

First of all, switch from an ArrayList to a List(Of String). ArrayList was already outdated in .NET 2.0, and you'll benefit a lot from strong typing.

Next, here's a method to chunk 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

Now, just use a For Each loop that iterates over the chunks:

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

Voilà!

Sign up to request clarification or add additional context in comments.

Comments

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.