Reputation: 45
I have 2 lists in my class which both contain strings, I am using winforms:
public List<Journey> journeys = new List<Journey>();
public List<Vehicle> vehicles = new List<Vehicle>();
I want to be able to take e.g the first element in the list journeys
and the first element in the list vehicles
, combine the data and then output that data to a listbox called ShowAll
.
I want to be able to do this with every item in the lists, so 2nd element in journeys
matches with 2nd element in vehicles
and 3rd element in journeys
matches with 3rd element in vehicles
etc.
I don't have a clue how to do this but it would be great if anyone could give me a hand. Thanks!
Upvotes: 0
Views: 97
Reputation: 4804
I didn't test this. I just wrote it here but it should work. Give it a shot.
Good Luck.
StringBuilder sb = new StringBuilder();
for(int n=0; n< journeys.Length; journeys++)
sb.Append(journeys[n]).Append(vehicles[n]).Append("\n");
ShowAll.Clear();
ShowAll.Text = sb.ToString();
Upvotes: 0
Reputation: 3833
Try something like
class CompositeObject //use a better name that fits your context..
{
Journey journey; public Journey Property { get; private set; }
Vehicle vehicle; public Vehicle Property { get; private set; }
public CompositeObject(Journey journey, Vehicle vehicle)
{
this.journey = journey;
this.vehicle = vehicle;
}
public override string ToString()
{
return this.vehicle.ToString() + " | " + this.journey.ToString();
}
}
now you can refactor the ToString
to give you output suitable for your gui (the listbox). and when it comes to writing into the listbox a simple for-loop should do
Upvotes: 1