在treenode中超出范围的例外 [英] Outofrange exeption in treenode

查看:131
本文介绍了在treenode中超出范围的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试循环遍历treeNodes时,我的范围错误。

这就是我的代码。我想跳过每个子节点的最后一个节点。



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.

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

推荐答案

问题是 x nodeList 上的一个计数器,但你也在 inTreeNode.Nodes 上使用它,这是另一回事。

你必须重新考虑你的意图。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。
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.


首先,困难可能在这里:
First, the difficulty may be here:
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);

您创建一个新的TreeNode,将其添加到集合中,然后将其重新分配给当前的slot 由同一集合中的索引值指向。为什么不直接将'tNode2直接传递给'AddNode'来调用?



并且可能添加一些代码来检查计数属性 inXmlNode集合和'inTreeNode集合对'x。

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.

var inXmlNodes = inXmlNode.ChildNodes;
int xmlNodeCount = inXmlNodes.Count();

var inTreeNodes = inTreeNode.Nodes;
if(inTreeNode.Count < xmlNodeCount { // throw an error }

如果这对您没有帮助,请在通话前设置一个断点到'AddNode并在达到该断点时遵循执行路径。

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.


这篇关于在treenode中超出范围的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆