Ingredients:

From the toolbox, drag a CheckedListBox to the main form, and a button. Ensure the listbox is large enough to view multiple items

DoubleClick the form to creat the load event. Add this code to the load event (this simply populates the CheckedListBox with entries)

CheckedListBox1.Items.AddRange( _

NewString() {“Apple”, “Orange”, “Banana”, _

“Avocado”, “Tomato”, “Tamarillo”, _

“Kiwifruit”, “Cherry”})

Create the click event for the button (in the code editors dropdowns, in the left drop down, select Button1) and add this code

MsgBox( _

Aggregate Box In CheckedListBox1.CheckedItems _

Into Concat())

At the very bottom of the code (after End Class) add this code. This code creates the aggregate ‘Concat’ function which enables this code to be used on the ‘Into’ above

PublicModule AggregateModule

<Extension()> PublicFunction Concat(Of Type)( _

ByVal ie As IEnumerable(Of Type)) AsString

Dim str AsString = “”

ForEach item In ie

If str <> “”Then str &= “,”

str &= item.ToString()

Next

Return str

EndFunction

EndModule

Alternatively, change the code in the button to be the following and remove the function we created (this is less code, but if you’re going to use this code a lot, then writing the Concat function is preferred)

One of the new things in Orcas is Lambda functions (or inline functions/BLOCKED EXPRESSION. Although

10 years ago

Bill_McC

oh, and although I assume that lambda does work in this case because each item is as String, the CheckListBox items might not be as string, so it’s probably safer if the code was

x.ToString & ", " & y.ToString

Also I presume that the code you showed would be reliant on Option Strict Off, as there is no way the compiler could infer the type to be as String becuase the collection is as Object.

10 years ago

Klaus Enevoldsen

I’m missing one ingredient: Visual Studio 2008 (Beta2 or Higher). 🙂

Anyway it would be better to check the length of the "str" variable in stead of comparing it to an empty string "".

Public Module AggregateModule

<Extension()> Public Function Concat(Of Type)( _

ByVal ie As IEnumerable(Of Type)) As String

Dim str As String = ""

For Each item In ie

If str.Length() > 0 Then str &= ","

str &= item.ToString()

Next

Return str

End Function

End Module

10 years ago

Kit George

Agreed on the length check, and agreed on the builder generally. For short concats, builders won’t make much difference of course, but if we’re building long strings, StringBuilder would be definitely preferred.

10 years ago

Ken Getz

This may be less efficient, but it makes me happier (and works with Option Strict On):

<Extension()> Public Function Concat(Of Type)( _

ByVal ie As IEnumerable(Of Type)) As String

Dim items As New List(Of String)

For Each item In ie

items.Add(item.ToString())

Next

Return String.Join(", ", items.ToArray())

End Function

I just hate that "check to ensure you haven’t already got an item to avoid the leading comma problem" issue. It just bugs me…

And I keep thinking that I should be able to cast ie As IEnumerable into List(Of String), to avoid adding each item in turn, but I sure can’t figure out the syntax.