Why does this code work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xmail123@yahoo.com

    #1

    Why does this code 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. I have two
    questions about why this code works.

    1. initTreeView(Tr eeNode 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?


    namespace DynamicTree
    {
    public partial class Form1 : Form
    {
    //Declare the table that will be populated with the Adjacency
    Matrix data.
    DataTable tbl;
    //Declare the coumns that will be populated in the Adjacency
    Matrix table.
    DataColumn col;

    public Form1()
    {
    InitializeCompo nent();
    //Create the Adjacency Matrix table.
    InitTable();
    //Populate the Adjacency Matrix table.
    initTableData() ;
    }

    #region InitTable() - Create the Adjacency Matrix table.

    private void InitTable()
    {//Create the Adjacency Matrix table.

    tbl = new DataTable();

    col = new DataColumn("ID" );
    tbl.Columns.Add (col);
    col = new DataColumn("PID ");
    tbl.Columns.Add (col);
    col = new DataColumn("Inf o");
    tbl.Columns.Add (col);

    tbl.AcceptChang es();

    }

    #endregion InitTable() - Create the Adjacency Matrix table.

    #region initTableData() - Populate the Adjacency Matrix table.

    private void initTableData()
    {//Populate the Adjacency Matrix table.
    DataRow r;

    r = tbl.NewRow();
    r["ID"] = "0";
    r["PID"] = "-1";
    r["Info"] = "Root";
    tbl.Rows.Add(r) ;


    r = tbl.NewRow();
    r["ID"] = "1";
    r["PID"] = "0";
    r["Info"] = "Menu1";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "10";
    r["PID"] = "0";
    r["Info"] = "Menu10";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "2";
    r["PID"] = "0";
    r["Info"] = "Menu2";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "3";
    r["PID"] = "0";
    r["Info"] = "Menu3";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "4";
    r["PID"] = "1";
    r["Info"] = "Menu4";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "5";
    r["PID"] = "4";
    r["Info"] = "Menu5";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "6";
    r["PID"] = "5";
    r["Info"] = "Menu6";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "7";
    r["PID"] = "2";
    r["Info"] = "Menu7";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "11";
    r["PID"] = "6";
    r["Info"] = "Menu11";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "8";
    r["PID"] = "10";
    r["Info"] = "Menu8";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "9";
    r["PID"] = "3";
    r["Info"] = "Menu9";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "12";
    r["PID"] = "7";
    r["Info"] = "Menu12";
    tbl.Rows.Add(r) ;

    r = tbl.NewRow();
    r["ID"] = "13";
    r["PID"] = "4";
    r["Info"] = "Menu13";
    tbl.Rows.Add(r) ;

    }

    #endregion initTableData() - Populate the Adjacency Matrix
    table.

    #region Form1_Load() - Create the root tree node.

    private void Form1_Load(obje ct sender, EventArgs e)
    {
    //Create the root tree node.
    TreeNode r = new TreeNode();
    r.Text = "Root";
    initTreeView(r) ;
    tree.Nodes.Add( r);
    tree.ExpandAll( );
    }

    #endregion Form1_Load()

    //The first pass the root tree node created in ther form load
    is passed.
    private void initTreeView(Tr eeNode N)
    {//This is the recursive method, calling it's self from the
    FOR loop at 201.
    //This creates nested tables containing the children for each
    node.

    //Create a temp table to act as a buffer to hold the
    //children of the current node.
    DataTable temp = new DataTable();
    col = new DataColumn("ID" );
    temp.Columns.Ad d(col);
    col = new DataColumn("PID ");
    temp.Columns.Ad d(col);
    col = new DataColumn("Inf o");
    temp.Columns.Ad d(col);
    temp.AcceptChan ges();

    //Retrieve the child ID of the current node.
    string id = getID(N);


    foreach (DataRow r1 in tbl.Rows)
    {//Step through the Adjacency Matrix table to find
    //all the children of the current node.

    if (r1["PID"].ToString() == id)
    {//This row represents a child of the current node.

    //Add a row to the buffer table to contain this
    child
    //of the of the current node.
    DataRow r2 = temp.NewRow();
    r2["ID"] = r1["ID"].ToString();
    r2["PID"] = r1["PID"].ToString();
    r2["Info"] = r1["Info"].ToString();
    temp.Rows.Add(r 2);
    temp.AcceptChan ges();
    }
    }

    foreach (DataRow r3 in temp.Rows)
    {//Step through the buffer table and create a node for
    each row.
    //These are the children of the current node.

    //If temp is empty control pops ouit and goes to
    initTreeView()
    //calls it's self, back to 157.

    TreeNode tn = new TreeNode();
    tn.Text = r3["Info"].ToString();

    //This is where initTreeView() calls it's self, back
    to 157
    //To create a nested table to hold the children of
    this node.
    initTreeView(tn );

    N.Nodes.Add(tn) ;
    }
    }

    //Return the child ID of the current node.
    private string getID(TreeNode N)
    {

    foreach (DataRow r in tbl.Rows)
    {
    //Step through the Adjacency Matrix table to find row
    //representing the current node. Return the child ID.
    if (r["Info"].ToString() == N.Text)
    return r["ID"].ToString();
    }
    return "";
    }
    }
    }


  • Carl Daniel [VC++ MVP]

    #2
    Re: Why does this code work?

    xmail123@yahoo. com wrote:
    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. I have two
    questions about why this code works.
    You should post this question in microsoft.publi c.dotnet.langua ges.csharp.
    This newsgroup is for C++.

    -cd


    Comment

    Working...