2

I am writing a function that iterates through string[] array and adds some objects into lists. It is a part of loading saved game system. It has a lot of buggs yet, because I wrote a lot of code, before can test any of it. But in this debugging phase I found some weird bug, that am not able to explain or fix.
I cant access some of data. If I have original code, for-loop iterates 1 time less then needed. If I add +1 to iterator limit, it acts normally. To conclude, I can iterate either -1 or +1 times, but not as much as I need.
This is the original code, which iterates one time less than expected:

string str = " start at " + Convert.ToInt32(s[k]) + " " + k;
        for (int i = 0; i < Convert.ToInt32(s[k]); i++)
        {
            k++;
            landOwned.Add(setle.civil.land[Convert.ToInt32(s[k]), Convert.ToInt32(s[k+1])]);
            k++;
            str += "  iteration "+i + " finish " + k;
        }
        throw new ArgumentException(str);

This is the line in text file:

f 4894 0 250 -523058 0 0 0 0 0 0 0 0 0 0 0 0 0 35 35 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 3 4 2 4 3 3 4 4 1 0 1 0 1 0 1 2

Here are screenshots of exeptions catched:

enter image description here

2
  • 3
    your for loop end point changes throughout the loop is that intended? Commented Jul 4, 2013 at 21:31
  • no it wasn't. I thought I pass only value, not a function Commented Jul 5, 2013 at 7:57

2 Answers 2

6

The problem is that a variable that is used in your loop end test (k) is incremented within the loop.

Changing the loop to the following should resolve the problem:

    var endIndex = Convert.ToInt32(s[k]);
    for (int i = 0; i < endIndex; i++)
Sign up to request clarification or add additional context in comments.

3 Comments

I should just go with gut instinct more often than waiting for more from OP; +1
U mean for-loop checks i < Convert.ToInt32(s[k]) every iteration? That would mean for-loop has to hold this function in call stack for every iteration D: I need to know more about this behavior. Always thought, that parameter of function is passed by value or reference, but not as a whole function. Should I start new question to know about this stuff, or someone can give a hint for what to search to know more about it?
@user1742303 - I've added an answer to explain
2

In answer to your comment,

you can treat a for loop as the following

int i = 0;
while(i < something){
//...


i++
}

every iteration, the check within the while loop will be checked again to see if I is still less than a value, this means in your situation it is converting "something" into an integer every iteration. @competent_techs solution will store the initial value of this into a separate variable that will then just store the value for the check to look at

1 Comment

Thank U very much. Now I understand that for-loop is not a method, but a packed up set of basic instructions (user level instructions). Something like inlined method.

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.