Connecting Tech Pros Worldwide Forums | Help | Site Map

String

shapper
Guest
 
Posts: n/a
#1: Sep 11 '07
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


Stephen
Guest
 
Posts: n/a
#2: Sep 11 '07

re: String


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.

Alexey Smirnov
Guest
 
Posts: n/a
#3: Sep 11 '07

re: String


On Sep 11, 10: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
Miguel, you can use StringBuilder

Dim MyItems As System.Text.StringBuilder = New
System.Text.StringBuilder()
Dim i As Integer

For i = 0 To items.Count - 1
MyItems.Append(list(i))
MyItems.Append(",")
Next

If MyItems.Length 0 Then
MyItems = MyItems.Substring(0, MyItems.Length - 1)
End If

(it's similar to one from Stephen)

Closed Thread