Click here to Skip to main content
15,905,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All ,
I am new to programming and i need you help to create this logic.

I have two classes Routes and Stops

there can be many stops at each route. Also each stop has it's own properties.
I have created a dictionary that has route as key and list of stops as value.
No problems there.

Now where i am getting stuck is that i have created a list of type stop and i am adding stop to that list for a route and this list will then be added to the dictionary as value and route will be the key. Problem is after first iteration of routes next time i will again have to add stop detail to the list which already has data. so records will be appended and whole process goes to waste where as in fact i have to add a new list to the dictionary as value.

how will i create a new list for every iteration. and i don't think creating a new list every time is a good idea. Please suggest what should i do.


 public Dictionary<string, List<Stops>> MyRoute = new Dictionary<string, List< Stops>>();
        public List<Stops> RouteStops = new List< Stops>();


class Stops
    {
        public String Id { get; set; }
        public string name { get; set; }
        public int sequence { get; set; }
        public string Key { get; set; }
    }
Posted

1 solution

Something like:
C#
public Dictionary<string, List<Stops>> MyRoute = new Dictionary<string, List< Stops>>();
public List<Stops> RouteStops = new List< Stops>();
public void MyMethod(int n)
   {
   int j = 0;
   for (int i = 0; i < n; i++)
      {
      RouteStops = new List<Stops>();
      Stop stop = new Stop();
      stop.Id = string.Format("{0}:{1}", i, j++);
      RouteStops.Add(stop);
      stop = new Stop();
      stop.Id = string.Format("{0}:{1}", i, j++);
      RouteStops.Add(stop);
      MyRoute.Add(i.ToString(), RouteStops);
      }
   }
 
Share this answer
 
Comments
Riya-Pandey 25-Aug-15 6:00am    
Thank you Mr OriginalGriff. i will work on this logic and will also try to see if it can be done some other way too. Thanks again for your help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900