[英]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. 为此,我在c#4.0中使用了
dynamic
返回类型。
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
部分:
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. 我认为这将消除当前的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.