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

Guest

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)
 

tvg

Joined
Mar 28, 2008
Messages
1
Reaction score
0
3.5 Framework makes this easy using Linq extensions:

Dim MyItems As String = String.Join(",", items.Select(Function(item) item.Product).ToArray())

and FYI in C#:

string MyItems = string.Join(",",items.Select(item => item.Product).ToArray());
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top