LINQ Cookbook, Recipe 5: Concatenating the selected strings from a CheckedListBox (Kit George)

LINQ Cookbook, Recipe 5: Concatenating the selected strings from a CheckedListBox (Kit George)

Rate This
  • Comments 7

Ingredients:

-          Visual Studio 2008 (Beta2 or Higher)

 

Categories: LINQ-To-Objects, LINQ and WinForms

 

Instructions:

  • Open Visual Studio 2008, and Click ‘File/New Project’. Create a new Windows Forms Application project
  • 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( _

  New String() {"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 

    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 <> "" Then str &= ","

          str &= item.ToString()

        Next

        Return str

      End Function

    End Module

    • 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) 

    MsgBox((From c In _

            CheckedListBox1.CheckedItems).Aggregate( _

       Function(ByVal x, ByVal y) x + "," + y))

    Leave a Comment
    • Please add 2 and 2 and type the answer here:
    • Post
    • Should use a StringBuilder instead of String in the Concat method.

      Which is kind of interesting becuase how do you use a string builder effectively with the lambda function approach ?

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

    • 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.

    • 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

    • 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.

    • 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.

    • nice variant, thanks Ken

    Page 1 of 1 (7 items)