Method not returning string value c# <List>

public List<string>  Test_IsDataLoaded()
    {
        try
        {
            if (GRIDTest.Rows.Count != 0)
            {
                int countvalue = GRIDTest.Rows.Count;
                GRIDTest.Rows[0].WaitForControlReady();

                List<string> ReleaseIDList = new List<string>();

                int nCellCount = GRIDTest.Cells.Count;

                for(int nCount = 0;nCount<nCellCount ;nCount++)
                  {
                        if(nCount %5==0)
                        ReleaseIDList.Add((GRIDTest.Cells[0].GetProperty("Value").ToString()));
                  }
                return ReleaseIDList;    
             }

        }
        catch (Exception)
    {
    }
}

Method throws me error = Not all code path return a value. Whats wrong in code.

Upvotes: 0

Views: 1935

Answers (4)

Akash KC
Akash KC

Reputation: 16310

The reason behind complaining above problem is that you are not returning the value from all over the method....It only returns from if condition but if it skip if statement, there'll be no returning value.So you've to be sure about returning value all over the method....

You can do like this:

public List<string>  Test_IsDataLoaded()
{
   List<string> ReleaseIDList = new List<string>();
    try
    {
        if (GRIDTest.Rows.Count != 0)
        {
            int countvalue = GRIDTest.Rows.Count;
            GRIDTest.Rows[0].WaitForControlReady();
            int nCellCount = GRIDTest.Cells.Count;

            for(int nCount = 0;nCount<nCellCount ;nCount++)
              {
                    if(nCount %5==0)
                    ReleaseIDList.Add((GRIDTest.Cells[0].GetProperty("Value").ToString()));
              }

         }
    }
    catch (Exception)
    {
    }
    return ReleaseIDList;  
}

Upvotes: 0

PHeiberg
PHeiberg

Reputation: 29811

The compiler is complaining because if an exception occurs or the if statement returns false, there will be no return statement executed.

Add a default return statement in the end of the method.

Upvotes: 0

Curtis
Curtis

Reputation: 103358

Your error is:

Not all code path return a value

Which is correct. You only return a value inside an if statement:

if (GRIDTest.Rows.Count != 0)

What if GRIDTest.Rows.Count==0. Then you won't return a value.


As a fail-safe (In case your code errors, or your if statement isn't true), you can add the following to the last line of your method:

return new List<string>();

This will ensure that if no other returns are made, then an empty List will be returned

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You add return in the end of your method

public List<string>  Test_IsDataLoaded()
    {
        try
        {
            if (GRIDTest.Rows.Count != 0)
            {
                int countvalue = GRIDTest.Rows.Count;
                GRIDTest.Rows[0].WaitForControlReady();

                List<string> ReleaseIDList = new List<string>();

                int nCellCount = GRIDTest.Cells.Count;

                for(int nCount = 0;nCount<nCellCount ;nCount++)
                  {
                        if(nCount %5==0)
                        ReleaseIDList.Add((GRIDTest.Cells[0].GetProperty("Value").ToString()));
                  }
                return ReleaseIDList;    
             }

        }
        catch (Exception)
    {
    }

return new List<string>() ;//<-------here
}

Upvotes: 0

Related Questions