4

I'm trying a simple comparison here, assignment doesn't work as i would like... here is the code,

int returnDateIndex(Paragraph para)
{
    long firstIndex = 0;
    for (int i = 0; i < para.Words.Count; i++)
    {
        if (para.Words[i].Text == "Second")
        {
            if (para.Words[i - 1].Text == "First")
            {                        
                firstIndex = para.Words[i].FirstSymbolPosition;
            }
        }
    }     
    return (int)firstIndex;
}

I ran my debugger (In VS) and when that assignment is called, the int on the right was equal to 50, but the int on the left stayed equal to 0. No idea what I'm missing.

This application is using the Abbyy FineReader 9.0 SDK and the documentation for FirstSymbolPosition says it returns a read-only Long

EDIT: the code has been stripped of all features to make it easier for viewers to see where the problem is. I would appreciate answers for the original questions and anything else with the code that is bugging you as a comment please.

1
  • And where do you break when you check the values? Commented Jul 6, 2010 at 16:59

4 Answers 4

3

One obvious error is that you're expecting to return the first case where your conditions match (hence the firstIndex variable name), but you're really returning the last point where they match. This is also bad because it means you keep looking after finding your match. Another is that if the very first word in a sentence is "Second", you'll try to reference a negative index, which is very bad. Try this instead:

int returnDateIndex(Paragraph para)
{
    for (int i = 1; i < para.Words.Count; i++) 
    {
        if (para.Words[i - 1].Text == "First" && para.Words[i].Text == "Second")                        
            return (int)para.Words[i].FirstSymbolPosition;
    }
    return 0; // this is what your original code would have returned in a "not found" scenario
}

This code fixes both errors, and also completely side-steps your assignment problem.

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

3 Comments

Thanks, but it only happens once in fact =) var names have been changed to suit the problem, sorry to confuse you.
What does 'i++ para.Words[i - 1].Text == "First"' mean?
Arrrrrrgh! :( Copy/paste error from the original. I shoulda spotted that much sooner. Fixed now.
1

As an aside you should add break after your assignment of firstIndex otherwise it could get overwritten by another value if the conditions are met later on in the loop.

3 Comments

Thanks for suggestion...this code has been minimalized for the post here though
Not to mention some additional condition so that if "Second" is the first word of the paragraph, it won't throw an ArrayIndexOutOfBoundsException.
Yup, i named it Second because its impossible for that word to come before the First word, i guess i should have mentioned i am dealing with very strong formatted files and as i mentioned i have an error check for that out of bound index.
1

In addition to what ChrisF said, this is pretty dangerous:

if (para.Words[i - 1].Text == "First")

There's no check in there to make sure i is greater than or equal to 1, if i is equal to 0 then you will be indexing out of the array.

1 Comment

Yeah again there are other checks in place this is just the barebones code with the problem still intact, thanks though
0

FYI: Comparison in .net doesn't always behave as you'd expect. Comparison of elementary types is different than complex types. If you compare integers you get a comparison of their content. If you compare objects you're comparing the address of the object NOT it's content.

1 Comment

what's this have to do with his question?

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.