none
How to use each expression to deal with List of List? RRS feed

  • Question

  • Dear All,

    I have implement a list of list class. or List<List<TreeNode>>


    I need to add the List<TreeNode> tn1 and List<TreeNode> tn2 as follows

    listlistnodes.l2.Add(tn1);

    listlistnodes.l2.Add(tn2);

    and print it by following does NOT work, How to correct it?

                foreach (List<TreeNode> l in listlistnodes)
                {
                    foreach(TreeNode n in l)
                    {
                        richTextBox1.AppendText(Environment.NewLine + n.Text);
                    }
                }

    Thanks and regards,

    E-John

    namespace WindowsFormsApp1ListOfList
    {
        public partial class Form1 : Form
        {
            public class ListListNodes
            {
                public List<List<TreeNode>> l2 = new List<List<TreeNode>>();
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ListListNodes listlistnodes = new ListListNodes();
    
                List<TreeNode> tn1 = new List<TreeNode>();
                for (int i = 0; i < 10; i++)
                {
                    TreeNode n = new TreeNode();
                    n.Text = "tn1-" + i.ToString();
                    tn1.Add(n);
                }
    
                List<TreeNode> tn2 = new List<TreeNode>();
                for (int i = 0; i < 10; i++)
                {
                    TreeNode n = new TreeNode();
                    n.Text = "tn2-" + i.ToString();
                    tn2.Add(n);
                }
    
                listlistnodes.l2.Add(tn1);
                listlistnodes.l2.Add(tn2);
    
                foreach (TreeNode n in tn1)
                {
                    richTextBox1.AppendText(Environment.NewLine + n.Text);
                }
                foreach (TreeNode n in tn2)
                {
                    richTextBox1.AppendText(Environment.NewLine + n.Text);
                }
            }
        }
    }
    


    Thursday, January 25, 2018 6:13 AM

Answers

  • Have you tried ‘foreach( List<TreeNode> l in listlistnodes.l2)’?

    • Marked as answer by E-John Friday, January 26, 2018 5:44 AM
    Thursday, January 25, 2018 6:38 AM

All replies

  • Have you tried ‘foreach( List<TreeNode> l in listlistnodes.l2)’?

    • Marked as answer by E-John Friday, January 26, 2018 5:44 AM
    Thursday, January 25, 2018 6:38 AM
  • Your immediate issue is that you are referring to your listlistnodes class rather than the actual List<List<TreeNode>> property l2 inside it (as Viorel_ points out).

    But as an alternative, have you considered inheriting your class from List<List<TreeNode>> rather than containing it as a property?

    This may or may not fit with whatever design you are aiming for, but generally implementing List<List<TreeNode>> as an inner property is only useful if you make it private so you can only allow access via specific methods and properties that you provide (encapsulation). But since you have made l2 public anyway...you could do:

    // Inherit directly from List<List<TreeNode>>
    public class ListListNodes: List<List<TreeNode>>
    {
    }

    Then when you add entries you can new this directly (no inner property l2):

    ListListNodes listlistnodes = new ListListNodes();
    
    List<TreeNode> tn1 = new List<TreeNode>;
    
    // populate tn1
    
    listlistnodes.Add(tn1)

    And with this syntax your original foreach code should work as you originally wrote it.

    Just a suggestion :)


    • Edited by RJP1973 Thursday, January 25, 2018 8:45 AM
    Thursday, January 25, 2018 8:44 AM
  • Dear Viorel,

    Yes, it works.

    Thanks and regards,

    E-John

                foreach (List<TreeNode> l in listlistnodes.l2)
                {
                    foreach (TreeNode n in l)
                    {
                        richTextBox1.AppendText(Environment.NewLine + n.Text);
                    }
                }

    Friday, January 26, 2018 5:44 AM
  • Dear RJP1973,

    Thanks for your suggestion. I will think it over if this way could meet our requirement or not.

    Thanks and regards,

    E-John

    Friday, January 26, 2018 5:48 AM