List<T> where T is a class with two members where one member is another list. How do I add/set variables?

213 Views Asked by At

I am new to C# and also to an extend to other object oriented languages, so excuse my ignorance. I am trying to create a List variable that is tied to a class with two members, such as the following:

List<bus> bus_list_2d = new List<bus>();

public class bus
{
  public string channel { get; set; }
  public List<String> label { get; set; }
}

I will be parsing through an xml document that has a channel assigned which holds multiple buffers labels. The exact number of channels is not known and same for the buffer labels, hence why I was wanting to use the List.

I was wondering how would I create the Add functionality where I would add a single channel and then keep on adding the label items and then move on to the next channel and add label items under that and so on. Can someone give me an example snippet for this please?

4

There are 4 best solutions below

0
clcto On

It is probably easier to add to a temporary variable and then add that to bus_list_2d:

bus b = new bus();
b.channel = "channel";
b.label = new List<string>();
b.label.Add( "label1" ); // this will probably be done in a loop of some kind

// ...

bus_list_2d.Add( b );

Also, you probably want to have the constructor of the bus class initialize label to a new List<string> so that you don't have to explicitly do it. Also please follow the naming conventions. Classes, Properties, and Methods are capitalized and use camel case.

public class Bus
{
    public string Channel { get; set; }
    public List<String> Label { get; set; }

    public Bus( string channel = "" )
    {
        this.Channel = channel;
        this.Label = new List<string>();
    }
}
0
Sergey Berezovskiy On

In C# we use PascalCase names for types and public members. So, it's better to use following names:

public class Bus
{
  public string Channel { get; set; }
  public List<string> Labels { get; set; }
}

Creating new list of buses will look like:

List<Bus> buses = new List<Bus>();
Bus bus1 = new Bus();
bus1.Channel = "Channel 6";
bus1.Labels = new List<string>();
bus1.Labels.Add("label A");
bus1.Labels.Add("label B");
buses.Add(bus1);

Also you can use object and collection initializers:

List<Bus> buses = new List<Bus> {
   new Bus {
      Channel = "Channel 6",
      Labels = new List<string> {
          "label A",
          "label B"
      }
   }
};
0
Yaugen Vlasau On

You can try something like this:

var busInfo = new Dictionay<string, List<string>>();


foreach(var channel in channels)
{
   busInfo.Add(channel, new List<string>());
   foreach(var label in channel.labels)
   {
      busInfo[channel].Add(label);
   }
}
0
John Alexiou On

Try using the params argument attribute for a method AddBus() to pass all the information with one line (see Program below).

public class Foo
{
    List<bus> bus_list_2d=new List<bus>();

    public void AddBus(string channel, params string[] labels)
    {
        bus_list_2d.Add(new bus()
        {
            channel=channel,
            label=new List<string>(labels)
        });
    }
}

public class bus
{
    public string channel { get; set; }
    public List<String> label { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Foo foo=new Foo();

        foo.AddBus("ABC", "A", "B", "C");
    }
}