0

I have been working through this tutorial:

http://www.codeproject.com/Articles/897559/Learn-MVC-in-days-Day

I have been getting not all code paths return a value on the "GetEmployees()" Method. As you can see I have also removed all the extra naming convention markups. I believe I am using a later version of .Net and visual studio was telling me they were causing errors.

For Example:

"public List<Employee><employee> GetEmployees()" to:  
"public List<Employee> GetEmployees()"

public class EmployeeBusinessLayer
{
    public List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee>();
        Employee emp = new Employee();
        emp.FirstName = "johnson";
        emp.LastName = " fernandes";
        emp.Salary = 14000;
        employees.Add(emp);

    }
}

Please help, I am really trying to get my head around the language of C# MVC, and OOP as compared to PHP procedural style which is my background.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
Sc07713
  • 11
  • 3
  • 1
    You don't return anything. And you also don't have any conditional statements. So the message is only slightly vague. You have one code path, which doesn't return a value. – GolezTrol Jan 06 '16 at 07:23
  • in the tutorial example is not complete, you can download the full example and it won't have any problem. link: http://www.codeproject.com/KB/aspnet/897559/Collections.zip – mohammad eslahi sani Apr 29 '16 at 23:59

5 Answers5

2

your methods return type is List<Employee>. It expects you to return a List<Employee> object. But you are not returning anything from your method of type List<Employee>.

following will fix your issue.

public List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee>();
        Employee emp = new Employee();
        emp.FirstName = "johnson";
        emp.LastName = " fernandes";
        emp.Salary = 14000;
        employees.Add(emp);

        return employees;
    }
Irshad
  • 3,071
  • 5
  • 30
  • 51
2

'Code paths', the message explained

The message means that your function possibly doesn't return anything. 'Code path' is about paths your code might follow. For instance, if you have an if statement, depending on the condition, the code inside it will be executed or not, so you have two code paths.

In your case don't have any conditional statements, so there is only one code path. The entire function will run from begin to end.

So the message is only slightly vague. You have one code path, which doesn't return a value.

Solution

The solution: Return something. Give the fact that the return type is List<Employee>, you probably meant to return the list of employees, so the function should end with one extra line:

return employees;

If you're only used to functions in PHP, you may still have seen this before. The concept of functions returning values is known in most, if not all, procedural languages (not to be confused with functional languages, which is something different!).

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

As the error says, You need to return the employees which is of type List<Employee>.

public List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee>();
        Employee emp = new Employee();
        emp.FirstName = "johnson";
        emp.LastName = " fernandes";
        emp.Salary = 14000;
        employees.Add(emp);
        return employees;
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You need to return a value. Look at the code below.

public List<Employee> GetEmployees()
{
    List<Employee> employees = new List<Employee>();
    Employee emp = new Employee();
    emp.FirstName = "johnson";
    emp.LastName = " fernandes";
    emp.Salary = 14000;
    employees.Add(emp);

    return employees;
}
Jaxter
  • 46
  • 5
0

As was previously mentioned, you need to have a return statement in your method. However, there is a more concise way of doing so by using a yield return statement.

public IEnumerable<Employee> GetEmployees()
{
    Employee emp = new Employee();
    emp.FirstName = "johnson";
    emp.LastName = " fernandes";
    emp.Salary = 14000;

    yield return emp;
}
NightOwl888
  • 55,572
  • 24
  • 139
  • 212