C# - Adding data to list inside list

26,489

Solution 1

You had several bad decisions, some were design flaws and some were minor C# naming convention violations.

Couple of worth mentions flaws:

  1. vehID should have been a string and not int (Example "XPT")
  2. Movment has Name, Value and Time. It doesn't have a list of Values and Times.

Creation:

List<Vehicle> vehicles = new List<Vehicle>();

Vehicle vehicle = new Vehicle()
{
    Id = "XPT",
    Description = "Average Car",
    Steps = new List<Step>()
    {
        new Step() {
            Name = "move car",
            Movements = new List<Movement>()
            {
                new Movement("engage 1st gear", 1, 1),
                new Movement("reach 10kph", 10, 5),
                new Movement("maintain 10kph", 10, 12),
            }
        },
        new Step() {
            Name = "stop car",
            Movements = new List<Movement>()
            {
                new Movement("reach 0kph", 10, 4),
                new Movement("put in neutral", 0, 1),
                new Movement("turn off vehicle", 0, 0),
            }
        }
    }
};
vehicles.Add(vehicle);

Entities:

public class Movement
{
    public string Name { get; set; }
    public double Values { get; private set; }
    public double Time { get; private set; }

    public Movement(string name, double values, double time)
    {
        Name = name;
        Values = values;
        Time = time;
    }
}

public class Step
{
    public string Name { get; set; }
    public IList<Movement> Movements { get; set; }
}

public class Vehicle
{
    public string Id { get; set; } // Should be changed to string
    public string Description { get; set; }
    public IList<Step> Steps { get; set; }
}

Solution 2

You should create your classes like the following:

public class criterias
{
    public double values { get; set; }
    public double time { get; set; }
}

public class movChannels
{
    public movChannels
    {
        criteria = new List<criterias>();
    }
    public string name { get; set; }
    public IList<criterias> criteria { get; set; }
}

public class stepsList
{
    public stepsList
    {
        stepChannelsCriteria = new List<movChannels>();
    }
    public string steps { get; set; }
    public IList<movChannels> stepChannelsCriteria { get; set; }
}

public class vehicles
{
    public vehicles
    {
        vehValCriteria = new List<stepsList>();
    }
    public int vehID { get; set; }
    public string vehDescription { get; set; }
    public IList<stepsList> vehValCriteria { get; set; }
    public movChannels movments { get; set; }
}
Share:
26,489
peetman
Author by

peetman

Updated on July 15, 2022

Comments

  • peetman
    peetman almost 2 years

    How can I add the following data on the table into a list called Vehicles?

    enter image description here

    public class criterias
    {
        public double values { get; set; }
        public double time { get; set; }
    }
    
    public class movChannels
    {
        public string name { get; set; }
        public IList<criterias> criteria = new List<criterias>();
    }
    
    public class stepsList
    {
        public string steps { get; set; }
        public IList<movChannels> stepChannelsCriteria = new List<movChannels>();
    }
    
    public class vehicles
    {
        public int vehID { get; set; }
        public string vehDescription { get; set; }
        public IList<stepsList> vehValCriteria = new List<stepsList>();
    }
    

    Now, how can I add the data that I have in the table shown into a list called Vehicles? I will create other vehicles later...

  • peetman
    peetman about 8 years
    cannot use the type var I don't know why
  • Orel Eraki
    Orel Eraki about 8 years
    @peetman, code updated. (Also added vehicles.Add(...);) at the end.
  • peetman
    peetman about 8 years
    thanks. if I have more than one criteria to each Movement should I just replace the Values and Time with another class as I suggested in the Question?
  • Orel Eraki
    Orel Eraki about 8 years
    @peetman, Indeed, Good job. If you still can't make that happend, tell me and i will refactor. Feel free to upvote and accept if the post answered your question.
  • peetman
    peetman about 8 years
    Thanks. How can access the created list in a another class(.cs)?
  • peetman
    peetman about 8 years
    Great. I was missing the public static void Main() now how can I access this list in another class. I have create this in a class called HelpClass and I am trying to access by using HelpClass.vehicles but it does not work.
  • Orel Eraki
    Orel Eraki about 8 years
    Different question on different thread. You can find many answers to this question on your favorite search engine (many of the answers will be directed to StackOverflow). Additionally, you can upvote and accept the answer if that helped you.