#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    184
    Rep Power
    29

    Question Unreachable code


    In my main form on a C# application I have, I put the code below in my FormLoad function.

    Code:
    if (treeView1.SelectedNode.IsSelected)
       treeView1.SelectedNode = null;
    When I do this any code I put after it, is ignored. Could someone explain why this is happening and if there is anything I can do about it other than make this if state the last item in my FormLoad function.

    :confused:
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Insufficient information

    Comments on this post

    • salem agrees : nice link :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    184
    Rep Power
    29
    Originally Posted by clifford
    I could say the same for your reply. So, what information do you need then. I gave you the code that was causing the problem. What else do you need? Do you need the treeview code? It is just standard code to populate the treeview, but I can give it to you if you think that will help.

    :confused:
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Just as a wild stab, is there any chance at all that treeView1.SelectedNode.IsSelected would ever evaluate to true? If not, then the line that follows will never be executed. Apparently, VS has decided that treeView1.SelectedNode.IsSelected will always evaluate to false. Just a wild guess.

    Are there other warnings? Is the situation one in which IsSelected, which I assume is initialized to false, never gets set to true? Or that the code which I assume you had written to set it to true would never do that? Or that there's an instantiation problem with either SelectedNode or treeView1 (in which case there should most definitely be other warnings or error messages).

    So some of the addition information we would need would have to do with how IsSelected is declared and updated, since it appears that this error/warning about unreachable code is not a direct cause but rather a side effect of the actual problem.

    You say this is in FormLoad. Could it be a question of the sequence in which objects are instantiated and that that property does not yet exist either by this if-statement or by the time you try to set it to true? Or that you're using IsSelected before it ever has a chance to be set to true? At any rate, it would seem that what you're trying to do in FormLoad may be a factor, so FormLoad could be part of the information we're missing.

    I had only started learning C# and have been away from it for several months, so I am not yet any kind of expert on it.
    Last edited by dwise1_aol; March 4th, 2013 at 01:17 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    184
    Rep Power
    29
    Originally Posted by dwise1_aol
    Just as a wild stab, is there any chance at all that treeView1.SelectedNode.IsSelected would ever evaluate to true? If not, then the line that follows will never be executed. Apparently, VS has decided that treeView1.SelectedNode.IsSelected will always evaluate to false. Just a wild guess.

    Are there other warnings? Is the situation one in which IsSelected, which I assume is initialized to false, never gets set to true? Or that the code which I assume you had written to set it to true would never do that? Or that there's an instantiation problem with either SelectedNode or treeView1 (in which case there should most definitely be other warnings or error messages).

    So some of the addition information we would need would have to do with how IsSelected is declared and updated, since it appears that this error/warning about unreachable code is not a direct cause but rather a side effect of the actual problem.

    You say this is in FormLoad. Could it be a question of the sequence in which objects are instantiated and that that property does not yet exist either by this if-statement or by the time you try to set it to true? Or that you're using IsSelected before it ever has a chance to be set to true? At any rate, it would seem that what you're trying to do in FormLoad may be a factor, so FormLoad could be part of the information we're missing.

    I had only started learning C# and have been away from it for several months, so I am not yet any kind of expert on it.
    Thanks for the nice informative answer. I am not an expert either especially on treeView controls, normally I rarely use them, but decided to this time. I doubt what I am doing in FormLoad other than that one statement is a factor, but I will post the code. I can also post the other code relating to the treeView component just to be sure. It really is not giving me a problem other than I was a bit surprised when I added a MessageBox after the treeView statement and nothing happened.

    I just commented everything else out in FormLoad and I get the same reaction so I know it is that treeView statement. In any case I have posted some of my code in case it might help.

    Code:
    private void Form1_Load(object sender, EventArgs e) 
    {                          
        this.Size = new Size(624, 500);              
       
        PopulateTree();                            
    
        //make this statement the last thing in FormLoad, anything beyond it will be ignored.             
       if (treeView1.SelectedNode.IsSelected)
         treeView1.SelectedNode = null;                                 
    }
    Code:
    private void PopulateTree()         
    {             
       treeView1.BeginUpdate();             
       treeView1.ShowRootLines = true;             
       treeView1.ShowLines = true; 
       treeView1.Nodes.Add("Fruit");          
       treeView1.Nodes[0].Nodes.Add("Apples");                
       treeView1.Nodes[0].Nodes.Add("Oranges");        
       treeView1.Nodes[0].Nodes.Add("Plums");         
            
       treeView1.Nodes.Add("Animals");                
       treeView1.Nodes[1].Nodes.Add("Dogs");        
       treeView1.Nodes[1].Nodes.Add("Cats");         
       treeView1.Nodes[1].Nodes.Add("Birds");             
       treeView1.Nodes[1].Nodes.Add("Humans");              
               
       treeView1.Nodes.Add("Program");              
       treeView1.Nodes[3].Nodes.Add("Build");             
       treeView1.Nodes[3].Nodes.Add("History");             
       treeView1.Nodes[3].Nodes.Add("Help");      //add sub-nodes to Help             
       treeView1.Nodes[3].Nodes[2].Nodes.Add("Index");             
       treeView1.Nodes[3].Nodes[2].Nodes.Add("Program WebSite");             
       treeView1.Nodes[3].Nodes[2].Nodes.Add("Helpful Links");            
       treeView1.Nodes[3].Nodes[2].Nodes.Add("About");            
       treeView1.EndUpdate();                     
    }
    Code:
    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
       
       switch (e.Node.Text)
        {            
             case "Fruit":
                   break;
             case default:
                   break;
        {
    }
    Code:
    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        TreeNode Node = treeView1.GetNodeAt(e.Location);
    
        if (Node != null && Node.Bounds.Contains(e.Location))
              treeView1.SelectedNode = Node;
        else if (Node != null && !Node.Bounds.Contains(e.Location))
              treeView1.SelectedNode = null;
        else
              treeView1.SelectedNode = null;
                               
    }
    
    private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
    {
         if(e.Node != null)
              treeView1.SelectedNode = e.Node;
                      
    }
    
    private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
    {
         if (e.Node != null)
               treeView1.SelectedNode = e.Node;
                
    }
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by Z.K.
    I could say the same for your reply. So, what information do you need then.
    Did you follow the link?

    Originally Posted by Z.K.
    I gave you the code that was causing the problem.
    No you gave the code you think is the problem; you don't know what the problem is, so clearly narrowing it down may be missing the target.

    The point is that it is not plausible that code following that fragment does not run unless the fragment itself cause the process to terminate. The condition only affect the the line following it, any subsequent code would be unconditional.

    Context is everything. For example you say the code is "ignored" but what does that mean? How do you know it is "ignored"? What is the execution path if it does not execute that? You said the subsequent code does not run. If no code runs after that then presumably your process stops? If not then something must be running. Have you stepped the code in the debugger to observe the code flow, and to verify the variable state? Have you switched exception trapping on in the debugger for all exceptions? This will cause the process to break and report debugging information at the point of exception rather than just terminating.

IMN logo majestic logo threadwatch logo seochat tools logo