Return 多值 C#
Return Multiple Values C#
我写了一个单独的 class 来保存我网站的权限。我在每个 class 中调用此 class。我无法从我的权限 class 中取回值。我将在下面向您展示我拥有的内容:
权限Class
public class Permissions
{
public static string selectedNumber = "";
public static string selectedName= "";
public static string selectedLocation= "";
public Info SetInfo(string selectedValue)
{
string selectInfo = "SELECT [Num] AS 'Number', [Name] AS 'Name', CONVERT(nvarchar(50),RTRIM(b.Num])) + ' - ' + CONVERT(nvarchar(50),b.Name) AS 'Location' "
+ "FROM [TBL_Info] a "
+ "left join [TBL_Where] b on (a.[ID] = b.[ID]) "
+ "WHERE a.ID = @ID";
sqlCmd = new SqlCommand(selectInfo, sqlConn);
sqlConn.Open();
sqlCmd.Parameters.AddWithValue("@ID", selectedValue);
SqlDataReader rdrInfo = sqlCmd.ExecuteReader();
if (rdrInfo.HasRows)
{
rdrInfo.Read();
selectedNumber = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Number")).ToString();
selectedName= rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Name")).ToString();
selectedLocation = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Location")).ToString();
}
sqlCmd.Connection.Close();
return new Info()
{
number= selectedNumber,
name= selectedName,
location= selectedLocation
};
}
public class Info
{
public String number{ get; set; }
public String name{ get; set; }
public String location{ get; set; }
}
}
我目前正在尝试在另一个 class 中这样调用它:
Classes.Permissions permission = new Classes.Permissions();
permission.SetInfo(selectedUserValue);
最终产品是我在 class 中设置了文本框 我正在尝试使用来自 permission.SetInfo()
的 3 return 值进行调用
目前我无法得到任何东西returned....我知道我做错了什么,很明显。那么有人可以给我一些关于如何实现这一目标的建议吗?
除了我对一些风格方面的问题以外,您的代码看起来应该可以工作(只要您的数据库中有数据)。要从一个方法中 return 多个值,创建一个新对象(即 Info
)来定义要 returned 的东西。
所以,你应该可以这样写:
Textbox txtBox;
var info = new Classes.Permissions().SetInfo(SelectedUserValue);
txtBox.Text =
info.number ?? "<null>" + " " +
info.name ?? "<null> + " " +
info.location ?? "<null>'
请注意,我使用了空合并运算符 (??
),因为 SetInfo
可以 return Info
的一个实例,其中所有成员都是 null
如果没有从您的数据库查询中 returned 行。
顺便说一下,从一个方法 return 多个值的另一种方法是使用 out
参数:
Textbox txtBox;
string number;
string name;
string location;
new Classes.Permissions().SetInfo(SelectedUserValue, out number, out name, out location);
然后你的 SetInfo
看起来像:
public void SetInfo(string SeelctedUserValue,
out string number, out string name, out string location)
{
//assign values to number,name and location
}
同一个对象的return多个个实例,比你的方法应该return
- 一个
IEnumerable
(即一个List
或Array
),
- 一个
Tuple<>
,
- 容器对象。
例如,如果您知道自己要 return 恰好三个,您可能希望使用您的方法 return Tuple<Info,Info,Info>
:
public Tuple<Info, Info, Info> SetInfo(string SeelctedUserValue)
{
//query db
return new Tuple<Info, Info, Info>(
new Info{ number = "number", name = "name", location="location},
new Info{ number = "number", name = "name", location="location},
new Info{ number = "number", name = "name", location="location});
}
我写了一个单独的 class 来保存我网站的权限。我在每个 class 中调用此 class。我无法从我的权限 class 中取回值。我将在下面向您展示我拥有的内容:
权限Class
public class Permissions
{
public static string selectedNumber = "";
public static string selectedName= "";
public static string selectedLocation= "";
public Info SetInfo(string selectedValue)
{
string selectInfo = "SELECT [Num] AS 'Number', [Name] AS 'Name', CONVERT(nvarchar(50),RTRIM(b.Num])) + ' - ' + CONVERT(nvarchar(50),b.Name) AS 'Location' "
+ "FROM [TBL_Info] a "
+ "left join [TBL_Where] b on (a.[ID] = b.[ID]) "
+ "WHERE a.ID = @ID";
sqlCmd = new SqlCommand(selectInfo, sqlConn);
sqlConn.Open();
sqlCmd.Parameters.AddWithValue("@ID", selectedValue);
SqlDataReader rdrInfo = sqlCmd.ExecuteReader();
if (rdrInfo.HasRows)
{
rdrInfo.Read();
selectedNumber = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Number")).ToString();
selectedName= rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Name")).ToString();
selectedLocation = rdrInfo .GetSqlString(rdrInfo .GetOrdinal("Location")).ToString();
}
sqlCmd.Connection.Close();
return new Info()
{
number= selectedNumber,
name= selectedName,
location= selectedLocation
};
}
public class Info
{
public String number{ get; set; }
public String name{ get; set; }
public String location{ get; set; }
}
}
我目前正在尝试在另一个 class 中这样调用它:
Classes.Permissions permission = new Classes.Permissions();
permission.SetInfo(selectedUserValue);
最终产品是我在 class 中设置了文本框 我正在尝试使用来自 permission.SetInfo()
目前我无法得到任何东西returned....我知道我做错了什么,很明显。那么有人可以给我一些关于如何实现这一目标的建议吗?
除了我对一些风格方面的问题以外,您的代码看起来应该可以工作(只要您的数据库中有数据)。要从一个方法中 return 多个值,创建一个新对象(即 Info
)来定义要 returned 的东西。
所以,你应该可以这样写:
Textbox txtBox;
var info = new Classes.Permissions().SetInfo(SelectedUserValue);
txtBox.Text =
info.number ?? "<null>" + " " +
info.name ?? "<null> + " " +
info.location ?? "<null>'
请注意,我使用了空合并运算符 (??
),因为 SetInfo
可以 return Info
的一个实例,其中所有成员都是 null
如果没有从您的数据库查询中 returned 行。
顺便说一下,从一个方法 return 多个值的另一种方法是使用 out
参数:
Textbox txtBox;
string number;
string name;
string location;
new Classes.Permissions().SetInfo(SelectedUserValue, out number, out name, out location);
然后你的 SetInfo
看起来像:
public void SetInfo(string SeelctedUserValue,
out string number, out string name, out string location)
{
//assign values to number,name and location
}
同一个对象的return多个个实例,比你的方法应该return
- 一个
IEnumerable
(即一个List
或Array
), - 一个
Tuple<>
, - 容器对象。
例如,如果您知道自己要 return 恰好三个,您可能希望使用您的方法 return Tuple<Info,Info,Info>
:
public Tuple<Info, Info, Info> SetInfo(string SeelctedUserValue)
{
//query db
return new Tuple<Info, Info, Info>(
new Info{ number = "number", name = "name", location="location},
new Info{ number = "number", name = "name", location="location},
new Info{ number = "number", name = "name", location="location});
}