考虑一下。
protected string Active_Frozen(string text, string color)
{
connection();
string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
SqlCommand cmd = new SqlCommand(query, conn);
if(query=="true")
{
text = "Active";
color = "Green";
}
else
{
text = "Frozen";
color= "Red";
}
return (text, color);
}
我想同时返回两个字符串:文本和颜色,但我不知道问题出在哪里。
错误@返回语句。
(参数) ? textcolor
不能将lambda表达式转换为'string'类型,因为它不是委托类型。
当你返回 两种 的东西,你需要声明你的函数为返回 两种 的东西。然而,你的函数被声明为返回 一个 字符串。
解决这个问题的一个方法是使用 Tuple<T1,T2>
:
Tuple<string,string> Active_Frozen(string text, string color) {
...
return Tuple.Create(text, color);
}
请注意,根据设计中对返回值的使用情况,返回颜色的名称,而不是颜色对象本身,可能并不理想。如果您希望返回颜色的对象表示而不是字符串,请将颜色对象的第二个类型参数改为 Tuple
或创建自己的类来表示文本及其颜色。
制作一个类,并从方法中返回一个类对象。
public class Container
{
public string text {get;set;}
public string color{get;set;}
}
方法。
protected Container Active_Frozen(string text, string color)
{
connection();
string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
SqlCommand cmd = new SqlCommand(query, conn);
if(query=="true")
{
Container c = new Container{text = "Frozen", color= "Red"};
}
else
{
Container c = new Container{text = "Frozen", color= "Red"};
}
return c;
}
你可以使用 out
参数。
protected string Active_Frozen(out string text, out string color)
{
connection();
string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
SqlCommand cmd = new SqlCommand(query, conn);
if(query=="true")
{
text = "Active";
color = "Green";
}
else
{
text = "Frozen";
color= "Red";
}
}
像这样调用。
string text;
string color;
Active_Frozen(out text, out color);
你必须返回一个数组,一个Tuple<string,string>甚至一个List<string>(谁知道呢,有一天你可能要返回更多的项目)。
protected string[] Active_Frozen(string text, string color)
{
// Code code
return new string[] {text, color};
}
protected Tuple<string, string> Active_Frozen(string text, string color)
{
// Code code
return new Tuple<string, string>(text, color);
}
protected List<string> Active_Frozen(string text, string color)
{
List toReturn = new List<string>();
// Code code
toReturn.Add(text);
toReturn.Add(color);
return toReturn;
}
你可以把热度提高,返回一个Hashtable,或者使用out参数。
protected Hashtable Active_Frozen(string text, string color)
{
// Code code
Hashtable values = new Hashtable();
if(query=="true")
{
values.Add("text", "Active");
values.Add("color", "Green";
}
else
{
// etc.
}
return values;
}
protected void Active_Frozen(out string text, out string color)
{
// This way, whenever you modify text or color, you will modify the actual instances you passed to Active_Frozen instead of copies
// and code code
}
我认为你可以使用一个字符串列表,你可以返回所有你想要的值。
protected list<string> Active_Frozen(string text, string color)
{
connection();
string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
SqlCommand cmd = new SqlCommand(query, conn);
if(query=="true")
{
text = "Active";
color = "Green";
}
else
{
text = "Frozen";
color= "Red";
}
list<string> liststring = new list<string> {text, color};
return liststring;
}
有多种方法可以实现这一点。
阵列
protected string[] Active_Frozen(string text, string color)
{
string [] returnVal=new string[2];
returnVal[0] = text;
returnVal[1] = color;
return returnVal;
}
结构
struct myReturnValues
{
public string text;
public string color;
}
protected myReturnValues Active_Frozen(string text, string color)
{
myReturnValues returnVal = new myReturnValues();
returnVal.text = text;
returnVal.color = color;
return returnVal;
}
输出参数
protected myReturnValues Active_Frozen(out string text, out string color)
{
text="new valuess";
color= "new color";
}