none
Save cars into List<Car> RRS feed

  • Question


  • Hello, need help with this issue:

    I  have:

    List<Car> car= new List<Car>();StreamReader read1 = new StreamReader(@"C:\Users\cars.txt");           

    while ((item = read1.ReadLine()) != null)            {               

    string[] items = item.Split(';'); 

    // Here need to fill data from string[] to List<Car>, BUT HOW ?                         }         

      carGrid.ItemsSource = car;

    and Class Car:

     

     public Car(string Maker, string typer, string body_typer, string colorr, string year_of_maker, string kwr, string equip_levelr, string pricer, string extrasr="None")        {            Make = Maker;            Type = typer;            BodyType = body_typer;            Color = colorr;            YearOfMake = year_of_maker;            Kw = kwr;            EquipLevel = equip_levelr;            Price = pricer;            Extras = extrasr;        }        public string Make        {            get;            set;        }        public string Type         {            get;            set;        }        public string BodyType        {            get;            set;        }        public string Color        {            get;            set;        }        public string YearOfMake        {            get;            set;        }        public string Kw        {            get;            set;        }        public string EquipLevel        {            get;            set;        }        public string Price        {            get;            set;        }        public string Extras        {           get;           set;     }

    In the source code I need to fill from string[] to List<Car>, but how ?

    Thx !

    Monday, May 11, 2015 6:03 PM

Answers

  • This looks like a homework assignment so I will not provide you the code but I will explain what you need to do.

    • The list stores objects of type Car so for each item you want to insert into the list you need to create an instance of the Car class.
    • For each Car instance you create you need to set the properties based upon whatever the data is you are reading from the file (we have no idea what that format is).
    • Once you have created and initialized your Car instance you can add it to the list using the Add method. (i.e car.Add(someNewCar)
    • It appears that you're ultimately trying to bind this list to a grid so build the list first and then after you've added all the items to it you can set the source property of the grid to the list of cars to have it update the UI (depends upon what UI technology you're using).

    I would recommend that you rename your list to represent the fact that it is a list of cars (we tend to use plural nouns for lists).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Blackwood Monday, May 11, 2015 6:41 PM
    • Marked as answer by Fred BaoModerator Tuesday, May 19, 2015 1:34 AM
    Monday, May 11, 2015 6:19 PM
    Moderator

All replies

  • Check this similar question/answer out.

    http://stackoverflow.com/a/3497829/1260204


    Mark as answer or vote as helpful if you find it useful | Igor

    Monday, May 11, 2015 6:18 PM
  • This looks like a homework assignment so I will not provide you the code but I will explain what you need to do.

    • The list stores objects of type Car so for each item you want to insert into the list you need to create an instance of the Car class.
    • For each Car instance you create you need to set the properties based upon whatever the data is you are reading from the file (we have no idea what that format is).
    • Once you have created and initialized your Car instance you can add it to the list using the Add method. (i.e car.Add(someNewCar)
    • It appears that you're ultimately trying to bind this list to a grid so build the list first and then after you've added all the items to it you can set the source property of the grid to the list of cars to have it update the UI (depends upon what UI technology you're using).

    I would recommend that you rename your list to represent the fact that it is a list of cars (we tend to use plural nouns for lists).

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Blackwood Monday, May 11, 2015 6:41 PM
    • Marked as answer by Fred BaoModerator Tuesday, May 19, 2015 1:34 AM
    Monday, May 11, 2015 6:19 PM
    Moderator