Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting out of range error when trying to loop through treeNodes.
This is what my code looks like. I wanted to skip the last node of each child node.

C#
var nodeList = inXmlNode.ChildNodes;
for (var x = 0; x <= nodeList.Count - 1; x++)
{
 var xNode = inXmlNode.ChildNodes[x];
 if (xNode.Attributes?["NAME"] != null && xNode.Attributes?["TYPE"] != null)
{

    var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, ObjectImageIndex, ObjectImageIndex);
    inTreeNode.Nodes.Add(tNode2);
    tNode2 = inTreeNode.Nodes[x]; // This is where the exception is thrown (x is 20) but inTreeNode.Nodes only has 1
    AddNode(xNode, tNode2);
}
else
{
    var xmlAttribute = xNode.Attributes?["Name"];
    if (xmlAttribute == null) continue;
    var tNode = new TreeNode(xmlAttribute.Value);
    //inTreeNode.Nodes.Add(tNode);  //BECAUSE I WANT TO SKIP THE LAST CHILD OF EACH NODE
    //tNode = inTreeNode.Nodes[x];
    //AddNode(xNode, tNode);
}
}


What I have tried:

I have tried setting level and checking for childnodes
Posted
Updated 6-Sep-16 0:43am
Comments
Philippe Mori 5-Sep-16 23:06pm    
Learn to use a debugger... If you got an exception at item 20, then you probably executed the else part the iteration just before and since node is not added ion that case, you have one less node than current x on next iteration.

The problem is that x is a counter on nodeList but you also use it on inTreeNode.Nodes which is another thing.
You have to rethink your intends.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
First, the difficulty may be here:
C#
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, ObjectImageIndex, ObjectImageIndex);
inTreeNode.Nodes.Add(tNode2);
tNode2 = inTreeNode.Nodes[x]; // why are you doing this ? ?
AddNode(xNode, tNode2);
You create a new TreeNode, add it to the collection, and then re-assign it to the current "slot" pointed to by the indexed value in the same collection. Why don't you just pass 'tNode2 directly to the call to 'AddNode ?

And, possibly, add some code that checks that the 'Count properties of the 'inXmlNode collection and the 'inTreeNode collection are valid for all possible values of 'x.
C#
var inXmlNodes = inXmlNode.ChildNodes;
int xmlNodeCount = inXmlNodes.Count();

var inTreeNodes = inTreeNode.Nodes;
if(inTreeNode.Count < xmlNodeCount { // throw an error }
If that doesn't help you, then put a break-point just before the call to 'AddNode and follow the execution path when that break-point is reached.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900