Results 1 to 4 of 4

Thread: listbox and string concat.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    listbox and string concat.

    I have
    Code:
              Dim items = String.Empty
                        For Each i In ListBox2.Items
                            items = i & ", " & items
                        Next
    As you can see the string will always END with a comma.

    This is fine because when i retrieve the string from the db, i just use the split function.
    BUT its something i want to stay away from.

    These are the ways I thought of removing it.
    1) do some string manipulation and remove the comma at the end.
    2) instead of for loop, do a normal loop with a counter. when the counter reaches the last one, throw a if statement and concatenate without the comma.

    so whats the best way

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: listbox and string concat.

    Programming does not exist in a vacuum. How would you do this with a pen and paper? Each time you add an item you would add a comma first, except if there were no items already in the list. The same goes here:
    vb.net Code:
    1. For Each i In ListBox2.Items
    2.     If items <> String.Empty Then
    3.         items &= ", "
    4.     End If
    5.  
    6.     items &= i
    7. Next i
    Note that that will get the list in the opposite order to what you currently have. If that's an issue then use a For instead of a For Each loop and work backwards.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: listbox and string concat.

    Quote Originally Posted by jmcilhinney
    Programming does not exist in a vacuum. How would you do this with a pen and paper? Each time you add an item you would add a comma first, except if there were no items already in the list. The same goes here:
    vb.net Code:
    1. For Each i In ListBox2.Items
    2.     If items <> String.Empty Then
    3.         items &= ", "
    4.     End If
    5.  
    6.     items &= i
    7. Next i
    Note that that will get the list in the opposite order to what you currently have. If that's an issue then use a For instead of a For Each loop and work backwards.
    Thanks for that.

    Works perfectly.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: listbox and string concat.

    I meant to mention in my previous post, I'd also recommend using a StringBuilder rather than String concatenation. That is especially true if you have a lot of items in your ListBox. Each time you use a concatenation operator (&) a new String object is created and the contents of the two operands copied into it. That makes for a lot of memory allocation and data copying very quickly, which is very inefficient. The StringBuilder is far more optimal for building strings.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width