1

This issue has me stumped. I have a variable I am sharing in a loop and it is not incrementing.

The variable in question is elrObject.currentLocation. There are two ways to increment it - either if the XML element is empty or if it's not.

Here is the code:

if (reader.Name == "Cell")
{  
    if (reader.IsEmptyElement)
    {       
        Response.Write("i ran<br>");
        elrObject.currentLocation++;
    }       
    else
    {
        while (reader.Read())
        {           
            if (reader.IsStartElement())
            {                                           
                //labels
                if (elrObject.currentLocation >= elrObject.index && elrObject.currentLocation <= elrObject.index + elrObject.colSpan)
                    Response.Write("i ran again<br>");

                Response.Write(elrObject.currentLocation + "<br>");
                elrObject.currentLocation++;

            }
            if (reader.Name == "Cell")
            break;
        }
    }
}

The output I am getting is:
0
1
2
3
5

The number 4 is when the XML element is empty and the top loop runs. I am incrementing the variable but it won't show me number 4, it skips it entirely and goes to 5. I am sure the upper loop is running properly and before the lower one because the following also runs:

I ran
I ran again

This confirms the upper loop is running before the lower one....yet the number 4 is skipping itself! I would really appreciate some help with this.

1
  • 2
    Have you tried running it in a debugger? Commented Feb 18, 2013 at 20:10

1 Answer 1

4

It's not supposed to show number 4. You don't have a

Response.Write(elrObject.currentLocation + "<br>");

in the

if (reader.IsEmptyElement)

block.

Sign up to request clarification or add additional context in comments.

9 Comments

agree, that would be the problem.
But on the next iteration of (reader.Name == "Cell"), when it enters the bottom loop, shouldn't it show it there? After all, the response.write is before the increment.
@johnhannigan You're incrementing it by: elrObject.currentLocation++; in the first block.
Right, I also increment it in the second block (at the end). But my problem here is that the second block never shows this variable as 4 even though it is shared between the loops. After it is incremented to 4, the next time the loop runs it goes to block 2 and it should be at 4 at that point.
@johnhannigan You wrote: After it is incremented to 4, the next time the loop runs it goes to block 2 - That's your mistake. It really goes to block 1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.