Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Adding data to list inside list

Tags:

c#

.net

list

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...

like image 357
peetman Avatar asked May 17 '16 08:05

peetman


2 Answers

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; }
}
like image 178
Orel Eraki Avatar answered Oct 03 '22 11:10

Orel Eraki


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; }
}
like image 31
Jamie Rees Avatar answered Oct 03 '22 12:10

Jamie Rees