locked
Format List Models from string array RRS feed

  • Question

  • User-2132497554 posted

    Hi Folks,

    Here is my simple code as below:

      static void Main(string[] args)
            {
                List<Experience> listExperience = new List<Experience>();
                Experience experience = new Experience();
                string[] lines = {"Experience","Google (Employers Name)","Software Engineer (Job Title)" ,"2015","2016", "Vodafone (Employers Name)", "Software Engineer (Job Title)" , "2013", "2014"};
    
                foreach(string item in lines)
                {
                    Console.WriteLine(item);
                    //experience.CompanyName = item;
                    //listExperience.Add(experience);
                }
                //Console.WriteLine(listExperience);
                Console.ReadLine();
            }
    
    //In Models:
        public class Experience
        {
            public string CompanyName { get; set; }
            public string JobTitle { get; set; }
            public string FromYear { get; set; }
            public string ToYear { get; set; }
        }

    The line of items seems ok but I want to display list inside a model (experience) and output like this:

    [0] => {CompanyName = "Google (Employers Name)", JobTitle = "Software Engineer (Job Title)" , FromYear = "2015", ToYear = "2016" }
    [1] = > {CompanyName = " Vodafone (Employers Name)", JobTitle = "Software Engineer (Job Title)" , FromYear = "2013", ToYear = "2014"} 

    How to list experience like the above output from a line of items? Any idea?

    I am waiting for your response.

    Thanks in Advance

    Thursday, November 12, 2020 6:28 AM

Answers

  • User475983607 posted

    The design is very fragile and depends in order.   You have to use standard head (and tail) pointers to frame the data.   I'm not using a tail as you did not define one so the code just goes to the end of the string.

    static void Main(string[] args)
    {
        List<Experience> listExperience = new List<Experience>();
                
        string[] lines = { 
            "Experience", 
            "Google (Employers Name)", "Software Engineer (Job Title)", "2015", "2016", 
            "Vodafone (Employers Name)", "Software Engineer (Job Title)", "2013", "2014" };
    
        //Console.WriteLine(lines.Length);
    
        int i = 0;
        int properties = 4;
        foreach (string line in lines)
        {
            i++;
            if(line == "Experience")
            {                   
                while (i < lines.Length)
                {
                    Experience experience = new Experience();
    
                    experience.CompanyName = lines[i];
                    experience.JobTitle = lines[i + 1];
                    experience.FromYear = lines[i + 2];
                    experience.ToYear = lines[i + 3];
    
                    listExperience.Add(experience);
                    i += properties;
                }
            }
    
        }
    
        foreach (Experience ex in listExperience)
        {
            Console.WriteLine(ex.CompanyName);
            Console.WriteLine(ex.JobTitle);
            Console.WriteLine(ex.FromYear);
            Console.WriteLine(ex.ToYear);
    
            Console.WriteLine();
            Console.WriteLine("---------------------");
            Console.WriteLine();
        }
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2020 2:05 PM