String

S

shapper

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
 
S

Stephen

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

Alexey Smirnov

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

String and Class 3
Generic List 3
Generic List to String 3
Property. Control or View State? 1
Generic List. Remove duplicate 2
Generic.List 2
Property. 3
Generic.List 1

Top