Reputation: 63
I am getting this rediculous error "unreachablecode detected" and clearly everything in in proper visibiliity...
Here is the code ..
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;
}
}
Does anyone have an idea of whats going on ? I thought perhaps it was the lack of a "break" so i threw two in here (i saw on a form thats what a solution was) but either way it doesnt matter and the error is still happening. Any suggestions would be great.
Upvotes: 0
Views: 164
Reputation: 63065
else
condition will never run since you have if (true)
i think based on your comments, you need to change the implimentation as below
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();
}
Upvotes: 2
Reputation: 402
the code
if (true)
will always be executed and there is no chance to else condition be executed.
Upvotes: 0
Reputation: 66449
You have if (true)
- was there some condition you actually wanted to test here?
if (true)
{
// this code will always run
}
else
{
// this code will never run
}
Upvotes: 10