1

I have a scenario where i have to return different type of objects according to a condition.

For that i have used dynamic return type in c# 4.0.

But i couldn't achieve that.

public dynamic ValidateUser(string UserName, string Password)
{
    string Result = string.Empty;

    Employees clsEmployee = new Employees();
    Customer clsCustomer = new Customer();

    sqlConnection = new SqlConnection(Connection());
    command = new SqlCommand("dbo.usp_ValidateUser", sqlConnection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add("@Username", SqlDbType.VarChar).Value = UserName;
    command.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
    sqlConnection.Open();

    SqlParameter newSqlParam = new SqlParameter();
    newSqlParam.ParameterName = "@Result";
    newSqlParam.SqlDbType = SqlDbType.NVarChar;
    newSqlParam.Direction = ParameterDirection.Output;
    newSqlParam.Size = 50;
    command.Parameters.Add(newSqlParam);

    SqlDataReader dr = command.ExecuteReader();
    Result = command.Parameters["@Result"].Value.ToString();

    if (Result == "Employee")
    {
        while (dr.Read())
        {
            clsEmployee.EmployeeId = (int)dr["EmployeeId"];
            clsEmployee.EmployeeName = (string)dr["EmployeeName"];
            clsEmployee.DepartmentName = (string)dr["DepartmentName"];
            clsEmployee.RoleName = (string)dr["RoleName"];
        }    
        return clsEmployee;
    }
    else if (Result == "Customer")
    {
        while (dr.Read())
        {
            clsCustomer.CustomerId = (int)dr["CustomerId"];
            clsCustomer.CustomerName = (string)dr["CustomerName"];
            clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
            clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
        }
        return clsCustomer;
    }


    //How to return???
}

When i try to return inside the condition it throws me

Not all code path return value' error.

Any solutions?

4

3 回答 3

1
switch (Result)
{
    case "Employee":
    {
        ...
        return ...
    }
    case "Customer":
    {
        ...
        return ....
    }
    default:
        return Result;
}
于 2013-09-28T07:23:55.990 回答
0

Just remove the else part:

else if (Result == "Customer")
{
    while (dr.Read())
    {
        clsCustomer.CustomerId = (int)dr["CustomerId"];
        clsCustomer.CustomerName = (string)dr["CustomerName"];
        clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
        clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
    }
    return clsCustomer;
}
return Result;
于 2013-09-28T07:03:05.377 回答
0

Santhosh,

This is not a proper way to handle different types. You could consider the implementation of an interface to achieve this. This make more readability and much more extensible. I believe that you are working on a support code, so that you may have limitation for achieving the new implementation.

I think you could change like this

public object ValidateUser(string UserName, string Password)
{
    object retVal= null;
    string Result = String.Empty;

    Employees clsEmployee = new Employees();
    Customer clsCustomer = new Customer();

    // get the data
    Result = command.Parameters["@Result"].Value.ToString();

    if (Result == "Employee")
    {
        while (dr.Read())
        {
            clsEmployee.EmployeeId = (int)dr["EmployeeId"];
            clsEmployee.EmployeeName = (string)dr["EmployeeName"];
            clsEmployee.DepartmentName = (string)dr["DepartmentName"];
            clsEmployee.RoleName = (string)dr["RoleName"];
        }

        retVal=clsEmployee;
    }
   if (Result == "Customer")
    {
        while (dr.Read())
        {
            clsCustomer.CustomerId = (int)dr["CustomerId"];
            clsCustomer.CustomerName = (string)dr["CustomerName"];
            clsCustomer.CustomerEmail = (string)dr["CustomerEmail"];
            clsCustomer.CustomerMobile = (string)dr["CustomerMobile"];
        }

        retVal=clsCustomer;
    }
     retVal=Result;
     return retVal;
    }

I think this will eliminates the current problem.

于 2013-09-28T07:14:35.943 回答