检测到无法访问的代码?

时间:2014-02-21 17:11:30

标签: c#

我收到了这个“无法检测到无法访问的代码”的错误错误,显然所有内容都处于适当的可见性状态......

这是代码..

int lastAppNum = 0;

//load the xml document            
XmlDocument xdoc = new XmlDocument();
xdoc.Load(GlobalVars.strXMLPath);

//add a app node
XmlNode newApp = xdoc.CreateNode(XmlNodeType.Element, "app", null);

//add the app number - this will be used in order to easily identify the app details (used for overwriting/saving)
XmlAttribute newNum = xdoc.CreateAttribute("num");

//in order to create a new number - search the last num and add one to it 
foreach (XmlNode xmlAppChild in xdoc.ChildNodes)
{         
    //if there are existing child nodes
    if (true)
    {                   
        //get the last childs num attribute
        lastAppNum = Convert.ToInt32(xmlAppChild.LastChild.Attributes["num"].Value);

        //add +1 to the last app num
        lastAppNum++;

        //add the new value to the attribute
        newNum.InnerText = lastAppNum.ToString();

        break;

    }else{
        //if there isnt an existing child node - set the num to 1
        lastAppNum = 1; <<where the error happens
        newNum.InnerText = lastAppNum.ToString();
        break;

    }

}

有没有人知道最新情况?我想也许是缺少一个“休息”,所以我在这里扔了两个(我在一个表格上看到的是什么解决方案),但无论哪种方式都没关系,错误仍在发生。任何建议都会很棒。

3 个答案:

答案 0 :(得分:10)

你有if (true) - 你真的想在这里测试一些条件吗?

if (true)
{
    // this code will always run
}
else
{
    // this code will never run
}

答案 1 :(得分:2)

由于您有else

if (true)条件永远不会运行

我认为根据您的意见,您需要更改下面的实施

if(xdoc.HasChildNodes)
{
    foreach (XmlNode xmlAppChild in xdoc.ChildNodes)
    {         

        //if there are existing child nodes                
        //get the last childs num attribute
        lastAppNum = Convert.ToInt32(xmlAppChild.LastChild.Attributes["num"].Value);

        //add +1 to the last app num
        lastAppNum++;

        //add the new value to the attribute
        newNum.InnerText = lastAppNum.ToString();

    }
}else
{

    //if there isnt an existing child node - set the num to 1
    lastAppNum = 1; 
    newNum.InnerText = lastAppNum.ToString();
}

答案 2 :(得分:0)

代码

  if (true)

将始终执行,并且没有机会执行其他条件。