I have a list:
|
For example I want to rename the mileage.
|
Thanks
|
|
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bryian TanPosted Sep 30, 2010, 9:53 PM
Add a property in the Car class. Here is a brief example.
public class Car
{
public Car()
{
//
// TODO: Add constructor logic here
//
}
public string Name{get ; set;}
public string Brand { get; set; }
public string Plate { get; set; }
public int Mileage { get; set; }
}
Test car
internal List
{
List
myCars.Add(new Car { Brand = "Toyota", Mileage = 10, Name = "Toyota", Plate = "P1234" });
myCars.Add(new Car { Brand = "BMW", Mileage = 500, Name = "BMW", Plate = "BMW1" });
myCars.Add(new Car { Brand = "Honda", Mileage = 10000, Name = "Honda", Plate = "112233" });
myCars.Add(new Car { Brand = "Kia", Mileage = 5678, Name = "Kia", Plate = "Kia123" });
return myCars;
}
protected void Page_Load(object sender, EventArgs e)
{
List
foreach (Car c in cars)
{
if (c.Plate == "BMW1") //let say we want to change the mileage for the car with the plate # BMW1
{
c.Mileage = 1000;
}
}
}