Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Noob with a question: Int assignment not working as expected

Tags:

c#

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.

like image 921
Josh K Avatar asked Jul 06 '10 16:07

Josh K


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


3 Answers

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.

like image 178
Joel Coehoorn Avatar answered Sep 21 '22 13:09

Joel Coehoorn


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.

like image 22
ChrisF Avatar answered Sep 25 '22 13:09

ChrisF


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.

like image 28
dcp Avatar answered Sep 24 '22 13:09

dcp