2014-02-21 83 views
0

我得到這個荒謬的錯誤「unreachablecode檢測」,並在適當的visibiliity顯然一切都在...檢測到無法檢測到的代碼?

這裏是代碼..

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; 

    } 

} 

任何人都不會有什麼事的想法的?我想也許是因爲缺乏一個「休息」,所以我在這裏扔了兩個(我在一個表格上看到了什麼是解決方案),但是無論哪種方式都沒有關係,並且錯誤仍在發生。任何建議都會很棒。

+6

'如果(真)'可能永遠是'真':) –

+0

@ user3302467:如果您設置正確的警告級別,無法訪問的代碼不是錯誤。這更多的是警告用戶,你可能寫了一些沒有意圖的東西。 –

+0

難道那不是所有的警告?爲什麼他們都應該被視爲錯誤? – paulm

回答

10

你有if (true) - 有沒有一些你真正想在這裏測試的條件?

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

代碼

if (true) 

就一定會執行,也沒有機會去別的條件執行。

+0

ohh這是有道理的。是的,我忘了在那裏拋出一個條件陳述。哎呦! – user3302467

+0

樂意幫忙!請標記答案是否正確) –

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(); 
} 
相關問題