String and Class

S

shapper

Hello,

I have a class, named MyClass, with 2 properties: Name and Text.

And I have a string as follows: "dog, new york, france , car"
Note that the words are separated by commas and that sometimes there
are spaces before and after a comma.

I want to get all words, i.e. "dog", "new york", "france" and "car",
and add them to a generic.list(of MyClass) assigning them to property
"Text".

How can I do this?

Thanks,
Miguel
 
R

RB

shapper said:
Hello,

I have a class, named MyClass, with 2 properties: Name and Text.

And I have a string as follows: "dog, new york, france , car"
Note that the words are separated by commas and that sometimes there
are spaces before and after a comma.

I want to get all words, i.e. "dog", "new york", "france" and "car",
and add them to a generic.list(of MyClass) assigning them to property
"Text".

How can I do this?

Thanks,
Miguel

Something like this (note, untested code, may not compile!

Assuming VB .Net...

Dim myList As ArrayList

Dim myString As String = "dog, new york, france , car"
Dim mySplitStringArray As String() = myString.Split(",")

Dim myTempString As String
Dim i As Integer
For Each myTempString In mySplitStringArray
MyClass o = new MyClass()
MyClass.Text = myTempString.Trim()
mylist.Add(MyClass)
Next


Hope that helps,

RB.
 
P

Phil H

Something like this (note, untested code, may not compile!
Assuming VB .Net...

....

MyClass o = new MyClass()

With all due respect, you're dead right that won't compile in VB.net,
it's C# syntax!

I think you mean

Dim o as new MyClass()
 
R

RB

Phil said:
With all due respect, you're dead right that won't compile in VB.net,
it's C# syntax!

I think you mean

Dim o as new MyClass()

Um, yes, that might prove a potential problem!!

I do my programming in VB.NET, but my unit tests in C#, so I think I got
a little confused - thanks for pointing that out :)

Cheers,

RB.
 

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 2
Dataset to Generic.List(Of MyClass) 4
Use of class function 2
CSV to List 7
String 1
Split 2
Property. Control or View State? 1
Average 1

Top