Question

Consider:

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

I want to return both strings: text and color, but I am not sure what the problem is.

Error @ return statement:

(parameter) ? text/color

Cannot convert lambda expression to type 'string' because it is not a delegate type

Was it helpful?

Solution

When you are returning two things, you need to declare your function as returning two things. However, your function is declared as returning one string.

One way to fix it is using Tuple<T1,T2>:

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

Note that returning the name of the color, rather than a color object itself, may not be ideal, depending on the use of the returned values in your design. If you wish to return an object representation of the color instead of a string, change the second type argument of the Tuple, or create your own class that represents the text and its color.

OTHER TIPS

Make a class and return a class object from the method:

public class Container
{
    public string text {get;set;}
    public string color{get;set;}
}

Method:

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

You can use out parameters:

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

Call it like this:

string text;
string color;

Active_Frozen(out text, out color);

You have to return an array, a Tuple<string, string> or even a List<string> (who knows, you may have to return more items some day).

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

You could bring the heat upper and return a Hashtable, or using out parameters.

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
}

I think you can use a list of strings and you can return all the values you want:

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;

}

There are multiple ways you can achieve this:

  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 (similarly a class object)

    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. Out parameters

    protected myReturnValues Active_Frozen(out string text, out string color)
    {
        text="new valuess";
        color= "new color";
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top