pryxen
pryxen

Reputation: 413

Array of objects within an array of objects

I want to make an array of objects but I find it difficult because I'm just a beginner in using C#. My array of objects is quite complicated because I have an elements in objects which require to have an array of objects too just like this.

array = [
    {
      "bch": "001",
      "branch": "AAA",
      "pdaccounts": [
            {"Name":"John Doe","Amount":1000.00},...
      ],
      "raccounts": [
            {"Name":"John Doess","Amount":1980.56},...
      ],
      "collapse":true
    },
    ...
];

Can anyone help me?

Upvotes: 0

Views: 169

Answers (2)

ProgrammingLlama
ProgrammingLlama

Reputation: 38870

There are many ways you can initalize a list and a nested list. Imagine this is my class. I've included a parameterless constructor for use with the object initializer pattern (although it isn't limited to parameterless constructors), a constructor that takes a name and a list object, and a constructor that takes a name and a params array of nodes.

For the sake of simplicity, I've made the class reference itself, but obviously you don't need to do that.

public class Node
{
    public Node() // you could pass nothing and set them manually or use the object initializer pattern
    {
    }

    public Node(string name, List<Node> nodes) // you could pass the name and an existing list
    {
        this.Name = name;
        this.Nodes = nodes;
    }

    public Node(string name, params Node[] nodes) // you could pass the name and a list of items (which can be called like Node("a", node, node, node)
    {
        this.Name = name;
        this.Nodes = nodes.ToList(); // needs    using System.Linq;    at the top of the file or namespace
    }

    public string Name { get; set; }
    public List<Node> Nodes { get; set; }
}

Examples:

// object initializer
var childChildChildChildNode = new Node
{
    Name = "ChildChildChildChild"
}

// constructor: string name, params Node[] nodes
var childChildChildNode = new Node("ChildChildChild", childChildChildChildNode);

// constructor: string name, List<Node> nodes
var childChildNode = new Node("ChildChild", new List<Node> { childChildChildNode });

// object initializer
var childNode = new Node
{
    Name = "Child",
    Nodes = new List<Node>()
};
// add items to the list of the child node
childNode.Nodes.Add(childChildNode);

// object initializer for node and list
var parentNode = new Node
{
    Name = "Parent",
    Nodes = new List<Node> { childNode }
};

// full object initializer
var otherParentNode = new Node
{
    Name = "Parent",
    Nodes = new List<Node>
    {
        new Node {
            Name = "Child",
            Nodes = new List<Node>
            {
                new Node
                {
                    Name = "ChildChild1"
                },
                new Node
                {
                    Name = "ChildChild2"
                }
            }
        }
    }
};

Note that this is not an exhaustive list of ways to initialize an object with arrays and nested arrays, just some examples to get you started.

Upvotes: 2

BoredTweak
BoredTweak

Reputation: 28

Consider the classes/objects you'll need to represent in code which will contain this information. You might be accustomed to a POCO class such as follows:

public class Human 
{
    public string Name { get; set; }
    public int Age { get; set; }
}

A class like the Human example above can contain other classes you've defined. Note the Cheese property in the CheeseBurger class below.

public class Cheese
{
    public int SmellFactor { get; set; }
}

public class CheeseBurger
{
    public Cheese CheeseType { get; set; }
}

This can readily apply to fit each of your properties "pdaccounts" and "raccounts".

Upvotes: 1

Related Questions