Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

String

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shapper - 11 Sep 2007 21:52 GMT
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 - 11 Sep 2007 22:03 GMT
> Hello,
>
[quoted text clipped - 29 lines]
>
> 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 - 11 Sep 2007 22:33 GMT
> Hello,
>
[quoted text clipped - 29 lines]
>
> 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)

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.