在c#中的函数中返回两个字符串

时间:2014-05-09 14:25:14

标签: c# return

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

我想要返回两个字符串:文本和颜色,不确定问题是什么。

错误@ return语句:

(参数)?文本/颜色

无法将lambda表达式转换为'string'类型,因为它不是委托类型

6 个答案:

答案 0 :(得分:8)

当你返回两个的东西时,你需要将你的函数声明为返回两个的东西。但是,您的函数被声明为返回一个字符串。

解决此问题的一种方法是使用Tuple<T1,T2>

Tuple<string,string> Active_Frozen(string text, string color) {
    ...
    return Tuple.Create(text, color);
}

请注意,根据设计中返回值的使用情况,返回颜色名称而不是颜色对象本身可能并不理想。如果您希望返回颜色的对象表示而不是字符串,请更改Tuple的第二个类型参数,或创建自己的表示文本及其颜色的类。

答案 1 :(得分:2)

创建一个类并从方法返回类对象:

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

答案 2 :(得分:1)

您可以使用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);

答案 3 :(得分:0)

你必须返回一个数组,一个元组甚至一个List(谁知道,你可能有一天必须返回更多项目。)

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
}

答案 4 :(得分:0)

我认为您可以使用字符串列表,并且可以返回所需的所有值

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;

}

我希望能帮助你。

答案 5 :(得分:0)

有多种方法可以实现这个目标

<强> 1.Array

protected string[] Active_Frozen(string text, string color)
{
 string [] returnVal=new string[2];
 returnVal[0] = text;
 returnVal[1] = color;
return returnVal;
}

<强> 2。 Struct(类似Class对象)

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

第3。输出参数

 protected myReturnValues Active_Frozen(out string text, out string color)
    {
text="new valuess";
color= "new color";
    }