On Sep 11, 3:52 pm, shapper <mdmo...@gmail.comwrote:
Quote:
Hello,
>
I have a class, Item, with 2 properties:
>
"ID" of type Int
>
"Product" of type String
>
I create a variable which is a Generic.List(Of Item):
>
Dim items As Generic.List(Of Item)
>
items.Add(10, "Book")
>
items.Add(20, "Car")
>
...
>
I want to create a string from this generic list that holds all the
items products separated by a comma:
>
MyItems = "Book,Car,..."
>
How can I do this?
>
The easiest way I can find is to create a for loop. But then I need to
remove the last comma.
>
Anyway, I am not sure if this is the best way to do this.
>
Thanks,
>
Miguel
One possible alternative, rather than using a Generic.List, create a
specific Collection-based class with the ToString method overloaded to
produce your comma separated string. While the interior will probably
be a FOR loop, it's black-boxed.
As for the issues about the last comma, try this logic:
dim b as boolean = false
dim output as string = ""
for each str in items
if b then output &= ", "
b=true
output &= str
next
Basically, you're putting the comma onto the output string BEFORE the
item, for every item but the first one.
Sorry, if this seems rambling, but I've taken a rather strong
antihistamine an hour or so ago.