
private bool IsDestinationNodeAChildOfdraggingNode(TreeNode draggingNode,TreeNode destinationNode) { if (draggingNode.Nodes.Count == 0) return false; else { if (draggingNode.Nodes.Contains(destinationNode)) return true; else { foreach (TreeNode node in draggingNode.Nodes) return IsDestinationNodeAChildOfdraggingNode(node,destinationNode); } } }解决方法 其实我觉得你的算法错了.如果你只是要查看第一个值,为什么还要用foreach语句呢?此外,返回后的所有其他内容都是多余的,使代码更难以遵循. 我怀疑你要做的是更像这样的事情:
private bool IsDestinationNodeAChildOfdraggingNode(TreeNode draggingNode,TreeNode destinationNode) { // special case,no children if (draggingNode.Nodes.Count == 0) return false; // check if the target is one of my children if (draggingNode.Nodes.Contains(destinationNode)) return true; // recursively check each of my children to see if one of their descendants is the target foreach (TreeNode node in draggingNode.Nodes) if (IsDestinationNodeAChildOfdraggingNode(node,destinationNode)) return true; // dIDn't find anything return false;} 有些人会坚持认为早期的回归是邪恶的,这将导致这个版本的功能:
private bool IsDestinationNodeAChildOfdraggingNode(TreeNode draggingNode,TreeNode destinationNode) { bool retVal = false; if (draggingNode.Nodes.Count != 0) { // check if the target is one of my children if (draggingNode.Nodes.Contains(destinationNode)) { retVal = true; } else { // recursively check each of my children to see if one of their descendants is the target foreach (TreeNode node in draggingNode.Nodes) if (IsDestinationNodeAChildOfdraggingNode(node,destinationNode)) { retVal = true; break; } } } return retVal;} 总结 以上是内存溢出为你收集整理的C#:并非所有代码路径都返回一个值全部内容,希望文章能够帮你解决C#:并非所有代码路径都返回一个值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)