Reputation: 23
Very new to C# and I'm trying to create a list of node data which contains a variable list length of Link data.
class Data
{
public List<Node> Node { get; set; }
}
public class Node
{
public string viewer { get; set; }
public int viewerId { get; set; }
public string log { get; set; }
public List <Link> Link { get; set; }
}
public class Link
{
public string keyName { get; set; }
public int value { get; set; }
}
i have a for loop iterating through the configured nodes and an inner for loop to grab any configured links.
Data data = new Data();
data.Node = new List<Node>();
I'm doing the following for each new node, which is working how i want it.
data.Node.Add( new Node {
viewer = setup.Device[moduleNr].viewer,
viewerId = setup.Device[moduleNr].viewerId ,
log = setup.Device[moduleNr].log
// how to add one or more lists of Link to this list???
});
The problem i'm having is adding a new list/lists inside the existing data.Node???
Ultimately i would like to achieve the following -
data
|->Node
|->[0]
|->Link
|->[0]
|->keyname
|->value
|->[1]
|->keyname
|->value
|->[2]
|->keyname
|->value
|->log
|->viewerId
|->viewer
|->[1]
|->Link
|->[0]
|->keyname
|->value
|->[1]
|->keyname
|->value
|->log
|->viewerId
|->viewer
|->[2]
|->Link
|->[0]
|->keyname
|->value
|->log
|->viewerId
|->viewer
Would really appreciate some help with this issue - Thanks
Upvotes: 2
Views: 366
Reputation: 8194
You can add a new instance of a List<Link>
like this and use the constructor to add new items:
data.Node.Add(new Node {
viewer = setup.Device[moduleNr].viewer,
viewerId = setup.Device[moduleNr].viewerId ,
log = setup.Device[moduleNr].log,
Link = new List<Link>
{
new Link
{
keyName = "Link 1",
value = 0
},
new Link
{
keyName = "Link 2",
value = 1
}
}
});
Upvotes: 2