none
Learning C# and struggling with Interface search and foreach code block

    Question

  • I'm new to C# and trying to implement my code to return an employee from an interface, at this point I'm beginning to confuse myself with what I'm doing, any suggestions?

    /// <summary>
            /// Find specific Employee in Directory
            /// </summary>
            /// <param name="employeeInfo">how employee is found, employee info</param>
            /// <returns>single employee name</returns>
            public IEmployee FindEmployee(IEmployee employeeInfo)
            {
                foreach (IEmployee employee in employees)
                {
                    if (employeeInfo.FirstName == employee.FirstName)
                    {
                        if (employeeInfo.LastName == employee.LastName)
                        {
                            if (employeeInfo.EmailAddress == employee.EmailAddress)
                            {
                                return employee;
                            }

                        }
                    }
                    else
                    {
                        return null;
                    }
                }


                //throw new NotImplementedException();
            }

       I continue to get a Not all code paths return a value error when compiling                    
    • Moved by litdev Wednesday, December 18, 2013 5:39 PM
    • Edited by CJ Evans 13 Wednesday, December 18, 2013 5:40 PM
    Wednesday, December 18, 2013 5:33 PM

Answers

  • Hi CJ,

    move the return outside the foreach loop. And you use && to spare some if:

            public IEmployee FindEmployee(IEmployee employeeInfo)
            {
                foreach (IEmployee employee in employees)
                {
                    if ((employeeInfo.FirstName == employee.FirstName) &&
                        (employeeInfo.LastName == employee.LastName) &&
                        (employeeInfo.EmailAddress == employee.EmailAddress))
                    {
                        return employee;
                    }
                }
                return null;
            }
    
    Regards, Elmar

    Wednesday, December 18, 2013 6:27 PM

All replies

  • Hi CJ,

    move the return outside the foreach loop. And you use && to spare some if:

            public IEmployee FindEmployee(IEmployee employeeInfo)
            {
                foreach (IEmployee employee in employees)
                {
                    if ((employeeInfo.FirstName == employee.FirstName) &&
                        (employeeInfo.LastName == employee.LastName) &&
                        (employeeInfo.EmailAddress == employee.EmailAddress))
                    {
                        return employee;
                    }
                }
                return null;
            }
    
    Regards, Elmar

    Wednesday, December 18, 2013 6:27 PM
  • Your error was cause because not all IF have else or there wasn't and default return like on the end of method
    Wednesday, December 18, 2013 7:35 PM
  • Thank you, I made the correction and now runs properly. Learning this is a work in progress, I appreciate the assistance.
    Thursday, December 19, 2013 4:42 PM