4

The code below is an exact copy of code that's working perfectly. The difference is that this code is being placed in a WCF Service Application Project whereas the working code is from a Windows Forms Application Project. The code in the foreach is unreachable which is strange because I've tested the code before and it works, returning the correct values

public IEnumerable<Employee> GetStudentDetails(string username,string password)
    {
        var emp = agrDb.LoginAuthentication(username, password);//procedure in the database thats returning two values
                                                                //Namely: EmployeeFirstName and EmployeeLastName
        List<Employee> trainerList = new List<Employee>();

        foreach (var item in emp)
        {
            //unreachable code here
            Employee employ = new Employee();
            employ.EmployeeFirstName = item.EmployeeFirstName;
            employ.EmployeeLastName = item.EmployeeLastName;
            trainerList.Add(employ);
            //trainerList.Add(item.EmployeeLastName);
        }
        return trainerList;
    }
Zubair
  • 107
  • 2
  • 12
  • 6
    Is this the compiler complaining that the code is unreachable? Or is it just not going into the loop? Can you create a short but *complete* program demonstrating the problem? – Jon Skeet Oct 25 '12 at 11:48
  • 1
    The only thing I can see is that emp must be an empty collection. That doesn't mean the code in the loop is unreachable, just that program flow doesn't go into the loop. – Roy Dictus Oct 25 '12 at 11:52
  • Give me a couple of minutes. I'll just create a small test to see if something is actually coming through – Zubair Oct 25 '12 at 11:53
  • @Jon Skeet, would the compiler ever figure that out for you? e.g. for(int i=0; i<0; i++){Console.WriteLine("Impossible");}, no compiler error or warning. – Justin Harvey Oct 25 '12 at 12:05
  • @JustinHarvey, Yes the compiler was telling me that the code was unreachable but for some reason it works regardless. Some new bugs have popped up but are easily fixable. Thanks JonSkeet. I would try to mark as Solved but don't know how from the comments – Zubair Oct 25 '12 at 12:10
  • Are you sure it was the compiler and not re-sharper or something else? – Justin Harvey Oct 25 '12 at 12:34
  • _Was_ the compiler telling you that the code is unreachable or _is_ the compiler still telling you that? – comecme Dec 06 '12 at 12:31

3 Answers3

6

The code within foreach loops can be uncreachable if the array or collection is not initialised until runtime.

List<Employee> emp;

// Run when program starts, called from Program.cs
private void InitialiseApplication()
{
    emp = new List<Employee>;

    // Gather data for employees from... somewhere.
    DataAccess.GetEmployees(emp);
}

private void DoStuff()
{
    foreach (var item in emp)
    {
         // Do something.
    }
}

The above code will bring back the warning because "emp" is not initialised at design time.

I've received the same warning in my code, at various stages including within constructors. However, the runtime flow is not affected because by then, "emp" is initialised.

This may be the case with your code. Check to see where and when during the flow of the program "emp" is initialised. You may need to "step into" the program to acheive this, if it is not obvious by sight.

ApacheTech
  • 61
  • 2
0

Maybe you are always returning a new empty array in agrDB.LoginAuthentication() and your IDE knows that the foreach loop will never iterate over any items.

Adrian Ciura
  • 1,102
  • 1
  • 9
  • 14
0

The code is reachable but if it is not executed this means that emp is empty. You have to verify if the value of username and password you are using exist and that agrDb.LoginAuthentication(username, password) is returning a value.

Moslem Ben Dhaou
  • 6,897
  • 8
  • 62
  • 93