简体   繁体   中英

Return any Type in C#

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?

switch (Result)
{
    case "Employee":
    {
        ...
        return ...
    }
    case "Customer":
    {
        ...
        return ....
    }
    default:
        return Result;
}

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM