1

条件に応じてさまざまな種類のオブジェクトを返さなければならないシナリオがあります。

そのためdynamicに、c# 4.0 で戻り値の型を使用しました。

しかし、私はそれを達成できませんでした。

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???
}

条件内に戻ろうとすると、スローされます

すべてのコード パスの戻り値ではありません」エラー。

解決策はありますか?

4

3 に答える 3

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

サントッシュ、

これは、さまざまなタイプを処理する適切な方法ではありません。これを実現するために、インターフェースの実装を検討できます。これにより、読みやすさと拡張性が向上します。あなたはサポート コードに取り組んでいると思いますので、新しい実装を実現するには限界があるかもしれません。

こんな感じに変えられると思います

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;
    }

これで今の悩みは解消されると思います。

于 2013-09-28T07:14:35.943 に答える
0

パーツを削除するだけelseです:

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 に答える