Adding items to a list of a custom datatype in C#

Hi everybody,

I was trying to add various values to a custom data type I created but for some reason just the first one gets added to the list.
I want to generate a adjacency list for nodes in a graph iterating over all edges and adding the neighbor.
If I do the same operation with a dataTree it works as expected.

 private void RunScript(DataTree<int> edges, int x, ref object A, ref object B)
  {
    var nodes = new List<Node>();

    var adj = new DataTree<int>();

    for (int i = 0; i < edges.BranchCount; i++)
    {
      // why does it not work like this
      Node n1 = new Node(edges.Branch(i)[0]);
      Node n2 = new Node(edges.Branch(i)[1]);
      nodes.Add(n1);
      nodes.Add(n2);

      n1.AdjN.Add(n2);
      n2.AdjN.Add(n1);

      //I thought it would be the equivalent to this:
      adj.Add(n1.name, new GH_Path(n2.name));
      adj.Add(n2.name, new GH_Path(n1.name));

    }




    // get the indices of the adjacent nodes
    var neighbors = new List<int>();
    for (int i = 0; i < nodes[x].AdjN.Count; i++)
      neighbors.Add(nodes[x].AdjN[i].name);

    A = neighbors;
    B = adj;
  }

  // <Custom additional code> 
  public class Node
  {
    //Properties
    public int name;
    public List<Node> AdjN;

    // constructor that takes the current node and initializes the adjList
    public Node(int _node)
    {
      name = _node;
      AdjN = new List<Node>();
    }
  }

If someone could give me a hint really apreciated.
Thanks for everyone and have a nice weekend!

File:20200625_CDT.gh (15.5 KB)

I haven’t drunk my coffee yet, so can you explain your problem better? What’s wrong? What do you expect? what does it have to do with being a custom type? Why do you use the x parameter to only access one node item?

Hi @Dani_Abalde,
thanks for reading and sorry for explaining so poorly.

Assuming I have a network of edges and I want to get a adjacency list for every vertex, I iterate over each edge putting the next vertex(edges.Branch(i)[1]) of the edge as a neighbor of the current vertex(edges.Branch(i)[0]) on it’s adjacency list and vice versa.

Using a datatree the outcome is a VertexConnectivityTree.Thats what I am after.

When I do the same with my Node object from my custom Node class(well I think it should be the equivalent action) instead of all adjacent vertices the adjacency list just contains one(the first) adjacent vertex.

Regarding your last two questions: I suspect it has to do with the custom data type, hence the title.
The x parameter I used to traverse the node list and show me just one node in the output.For debugging you could say.

I hope I did better explaining this time.
Thanks again !

20200625_CDT.gh (15.4 KB)

Hi @Dani_Abalde,
Thanks for your answer.
My aim is not to use the datatree. It was just a reference, and I used it to prove the adjacency thing works as I imagined. I want to do it with the node class only(as in the second screenshot of my previous post). I wonder why there is just one neighbor added to the neighbors list.
Thanks again for your effort!

You create a new Node, call the node.AdjN.Add() method on it once, then forget about that node. I may be misunderstanding this entirely, but are you expecting there to be more than one value in the AdjN lists?

Hi @DavidRutten,
Thanks for your answer.
You are understanding it right.

I think I understood what you mean. The correct way would be to add all nodes to a list and then add their neighbours to them. At the moment I can’t try that, but coming home I will.

Thank you both ,
Now it is clear to me, I should have seen it in @Dani_Abalde example , after @DavidRutten advice now I can see it. Thank you both again for having the patience!