mardi 10 septembre 2019

For loop does not functioning as expected in C#

I am checking multiple else if conditions within a for loop, but on debugging the if condition gets hit only once and no more looping takes place

Basically I am looping through excel columns, and checking the value for each cell in the particular column.The looping is not functioning as expected and just after the first iteration of the nested for loop the debugger comes straight out of the main for loop.

Below mentioned code can provide a better understanding

colcount here is 10, rowcount is 5.

for (int c = 1; c <= colcount; c++)
{
    for (int r = 2; r <= rowcount; r++) 
    {
        string celldata = usedRange.Cells[r,c].Text;
        if (c == 1)
        {
            if (SomeList1.Contains(celldata))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (c == 2)
        {
            if (SomeList2.Contains(celldata))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (c == 3)
        {
            if (celldata.Length <=10 && regex.IsMatch(celldata))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Can't figure out if I am making some basic mistake.

Aucun commentaire:

Enregistrer un commentaire