I have a list:
Code |
For example I want to rename the mileage.
Code |
Thanks
Code |
Code |
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bryian TanRank #396 · 6.3K reputationPosted Sep 30, 2010, 5:53 PM EDT
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;
}
}
}