473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Why does this work?

I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view.

1. initTreeView(TreeNode N) creates a new “temp” table to hold the children for each node. Each time the table is created it has the same name “temp”. Why doesn’t the table just get over written each time?
2. In the foreach (DataRow r3 in temp.Rows) loop, if the temp table is empty like when Menu11 is reached the call to initTreeView(tn) is not executed but for all the parent nodes Menu1, Menu4, Menu5 and Menu6 initTreeView(tn) is executed. There is no code to indicate that at Menu11 not to execute initTreeView(tn) How does C# know when to execute initTreeView(tn) and when not to?

Expand|Select|Wrap|Line Numbers
  1. namespace DynamicTree
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         //Declare the table that will be populated with the Adjacency Matrix data. 
  6.         DataTable tbl;
  7.         //Declare the coumns that will be populated in the Adjacency Matrix table. 
  8.         DataColumn col;
  9.  
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.             //Create the Adjacency Matrix table.
  14.             InitTable();
  15.             //Populate the Adjacency Matrix table.
  16.             initTableData();
  17.         }
  18.  
  19.         #region InitTable() - Create the Adjacency Matrix table.
  20.  
  21.         private void InitTable()
  22.         {//Create the Adjacency Matrix table.
  23.  
  24.             tbl = new DataTable();
  25.  
  26.             col = new DataColumn("ID");
  27.             tbl.Columns.Add(col);
  28.             col = new DataColumn("PID");
  29.             tbl.Columns.Add(col);
  30.             col = new DataColumn("Info");
  31.             tbl.Columns.Add(col);
  32.  
  33.             tbl.AcceptChanges();
  34.  
  35.         }
  36.  
  37.         #endregion InitTable() - Create the Adjacency Matrix table.
  38.  
  39.         #region initTableData() - Populate the Adjacency Matrix table.
  40.  
  41.         private void initTableData()
  42.         {//Populate the Adjacency Matrix table.
  43.             DataRow r;
  44.  
  45.             r = tbl.NewRow();
  46.             r["ID"] = "0";
  47.             r["PID"] = "-1";
  48.             r["Info"] = "Root";
  49.             tbl.Rows.Add(r);
  50.  
  51.  
  52.             r = tbl.NewRow();
  53.             r["ID"] = "1";
  54.             r["PID"] = "0";
  55.             r["Info"] = "Menu1";
  56.             tbl.Rows.Add(r);
  57.  
  58.             r = tbl.NewRow();
  59.             r["ID"] = "10";
  60.             r["PID"] = "0";
  61.             r["Info"] = "Menu10";
  62.             tbl.Rows.Add(r);
  63.  
  64.             r = tbl.NewRow();
  65.             r["ID"] = "2";
  66.             r["PID"] = "0";
  67.             r["Info"] = "Menu2";
  68.             tbl.Rows.Add(r);
  69.  
  70.             r = tbl.NewRow();
  71.             r["ID"] = "3";
  72.             r["PID"] = "0";
  73.             r["Info"] = "Menu3";
  74.             tbl.Rows.Add(r);
  75.  
  76.             r = tbl.NewRow();
  77.             r["ID"] = "4";
  78.             r["PID"] = "1";
  79.             r["Info"] = "Menu4";
  80.             tbl.Rows.Add(r);
  81.  
  82.             r = tbl.NewRow();
  83.             r["ID"] = "5";
  84.             r["PID"] = "4";
  85.             r["Info"] = "Menu5";
  86.             tbl.Rows.Add(r);
  87.  
  88.             r = tbl.NewRow();
  89.             r["ID"] = "6";
  90.             r["PID"] = "5";
  91.             r["Info"] = "Menu6";
  92.             tbl.Rows.Add(r);
  93.  
  94.             r = tbl.NewRow();
  95.             r["ID"] = "7";
  96.             r["PID"] = "2";
  97.             r["Info"] = "Menu7";
  98.             tbl.Rows.Add(r);
  99.  
  100.             r = tbl.NewRow();
  101.             r["ID"] = "11";
  102.             r["PID"] = "6";
  103.             r["Info"] = "Menu11";
  104.             tbl.Rows.Add(r);
  105.  
  106.             r = tbl.NewRow();
  107.             r["ID"] = "8";
  108.             r["PID"] = "10";
  109.             r["Info"] = "Menu8";
  110.             tbl.Rows.Add(r);
  111.  
  112.             r = tbl.NewRow();
  113.             r["ID"] = "9";
  114.             r["PID"] = "3";
  115.             r["Info"] = "Menu9";
  116.             tbl.Rows.Add(r);
  117.  
  118.             r = tbl.NewRow();
  119.             r["ID"] = "12";
  120.             r["PID"] = "7";
  121.             r["Info"] = "Menu12";
  122.             tbl.Rows.Add(r);
  123.  
  124.             r = tbl.NewRow();
  125.             r["ID"] = "13";
  126.             r["PID"] = "4";
  127.             r["Info"] = "Menu13";
  128.             tbl.Rows.Add(r);
  129.  
  130.         }
  131.  
  132.         #endregion initTableData() - Populate the Adjacency Matrix table.
  133.  
  134.         #region Form1_Load() - Create the root tree node.
  135.  
  136.         private void Form1_Load(object sender, EventArgs e)
  137.         {
  138.             //Create the root tree node.
  139.             TreeNode r = new TreeNode();
  140.             r.Text = "Root";
  141.             initTreeView(r);
  142.             tree.Nodes.Add(r);
  143.             tree.ExpandAll();
  144.         }
  145.  
  146.         #endregion Form1_Load()
  147.  
  148.         //The first pass the root tree node created in ther form load is passed.
  149.         private void initTreeView(TreeNode N)
  150.         {//This is the recursive method, calling it's self from the FOR loop at 201.
  151.          //This creates nested tables containing the children for each node.
  152.  
  153.             //Create a temp table to act as a buffer to hold the 
  154.             //children of the current node.
  155.             DataTable temp = new DataTable();
  156.             col = new DataColumn("ID");
  157.             temp.Columns.Add(col);
  158.             col = new DataColumn("PID");
  159.             temp.Columns.Add(col);
  160.             col = new DataColumn("Info");
  161.             temp.Columns.Add(col);
  162.             temp.AcceptChanges();
  163.  
  164.             //Retrieve the child ID of the current node.
  165.             string id = getID(N);
  166.  
  167.  
  168.             foreach (DataRow r1 in tbl.Rows)
  169.             {//Step through the Adjacency Matrix table to find 
  170.              //all the children of the current node.
  171.  
  172.                 if (r1["PID"].ToString() == id)
  173.                 {//This row represents a child of the current node.
  174.  
  175.                     //Add a row to the buffer table to contain this child 
  176.                     //of the of the current node. 
  177.                     DataRow r2 = temp.NewRow();
  178.                     r2["ID"] = r1["ID"].ToString();
  179.                     r2["PID"] = r1["PID"].ToString();
  180.                     r2["Info"] = r1["Info"].ToString();
  181.                     temp.Rows.Add(r2);
  182.                     temp.AcceptChanges();
  183.                 }
  184.             }
  185.  
  186.             foreach (DataRow r3 in temp.Rows)
  187.             {//Step through the buffer table and create a node for each row.  
  188.              //These are the children of the current node.
  189.  
  190.                 //If temp is empty control pops ouit and goes to initTreeView() 
  191.                 //calls it's self, back to 157.
  192.  
  193.                 TreeNode tn = new TreeNode();
  194.                 tn.Text = r3["Info"].ToString();
  195.  
  196.                 //This is where initTreeView() calls it's self, back to 157
  197.                 //To create a nested table to hold the children of this node.
  198.                 initTreeView(tn);
  199.  
  200.                 N.Nodes.Add(tn);
  201.             }
  202.         }
  203.  
  204.         //Return the child ID of the current node.
  205.         private string getID(TreeNode N)
  206.         {
  207.  
  208.             foreach (DataRow r in tbl.Rows)
  209.             {
  210.                 //Step through the Adjacency Matrix table to find row 
  211.                 //representing the current node. Return the child ID.
  212.                 if (r["Info"].ToString() == N.Text)
  213.                     return r["ID"].ToString();
  214.             }
  215.             return "";
  216.         }
  217.     }
  218. }
Oct 26 '07 #1
3 1273
Plater
7,872 Expert 4TB
1. initTreeView(TreeNode N) creates a new “temp” table to hold the children for each node. Each time the table is created it has the same name “temp”. Why doesn’t the table just get over written each time?
2. In the foreach (DataRow r3 in temp.Rows) loop, if the temp table is empty like when Menu11 is reached the call to initTreeView(tn) is not executed but for all the parent nodes Menu1, Menu4, Menu5 and Menu6 initTreeView(tn) is executed. There is no code to indicate that at Menu11 not to execute initTreeView(tn) How does C# know when to execute initTreeView(tn) and when not to?
1) The scope changes each time initTreeView() is called, A new object called "temp" is created for the scope. The old object is still there on the stack, but is out of scope until control returns to it's instance of initTreeView() (and will be removed when that instance calls "return" as it's stack is wiped out)

2) The table will be empty if it cannot find any children in the "tbl" table for the item, since it will only populate that temp table with rows when it finds a child for the current node. If menu11 as no children (no rows in "tbl" list PID as pointing to menu11), the table will be empty and it will not get called.


This is certainly an interesting exercise in tree building.
Oct 26 '07 #2
1) The scope changes each time initTreeView() is called, A new object called "temp" is created for the scope. The old object is still there on the stack, but is out of scope until control returns to it's instance of initTreeView() (and will be removed when that instance calls "return" as it's stack is wiped out)

2) The table will be empty if it cannot find any children in the "tbl" table for the item, since it will only populate that temp table with rows when it finds a child for the current node. If menu11 as no children (no rows in "tbl" list PID as pointing to menu11), the table will be empty and it will not get called.


This is certainly an interesting exercise in tree building.

Of what i could find this seemed to be the most direct way. I am open to suggestions if u have other methods. :)
Oct 26 '07 #3
Plater
7,872 Expert 4TB
Of what i could find this seemed to be the most direct way. I am open to suggestions if u have other methods. :)
No no, I like it. It makes use of a lot of techniques. Self-building menus can be very useful in "user-friendly" products.
Oct 29 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.