-1

I'm using a string which contains multiple values inside: Example

string Response="John,13,1st,Mike,15,3st"...and so on 

This 3 values need to be insert each value in separate class object. Example

class Students { public string Name { get; set; } public string Age { get; set; } public string SchoolClass{ get; set; } } 

I need to use my list as example

 Students mItems = new List<Students>(); mItems.Add(new Students() { Name = first string value,Age=second,SchoolClass=third....and so on 

Or if it is simpler using my string with key value method

Name:John,Age:13,SchoolClass:1st ..... and so on

7
  • If value order is certain then use Split method and assign values to properties from array. Commented Sep 25, 2018 at 12:41
  • 2
    So, what is your question? What problem did you experience? You now just described a task. Commented Sep 25, 2018 at 12:41
  • Yes order is specific. How i will do that? Is it better to use key value method? Commented Sep 25, 2018 at 12:41
  • 1
    Sounds like you need a CSV parser Commented Sep 25, 2018 at 12:41
  • public string Age { get; set; } type int would make more sense here Commented Sep 25, 2018 at 12:42

1 Answer 1

0

String.Split and a for-loop:

List<Students> studentList = new List<Students>(); string[] tokens = Response.Split(','); for(int i = 0; i <= tokens.Length - 3; i+=3) { Students s = new Students {Name = tokens[i], Age = tokens[i + 1], SchoolClass = tokens[i + 2]}; studentList.Add(s); } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you nice example!
@iBug: i noticed and flagged as spam. Btw, why there is there still no way to PM/notify someone?
@iBug: then you don't need to comment on a random post if you want to talk to someone. Especially because this isn't related to the question or answer and it's not interesting for others.
@TimSchmelter I'm fine with deleting these comments afterwards. I just like to remind others of inappropriate actions taken.
@iBug: sure, that workaround "works" but i don't understand why SO doesn't let communicate users directly (you could block someone if you get annoyed).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.