7

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nithraw
  • 67
  • 1
  • 8
  • Encapsulate the properties `Text` and `Color` in a class and return an instance of the class. The syntax that you describe is not available in C#. – User 12345678 May 09 '14 at 14:26
  • 1
    You can return a `Tuple` or (preferably) just roll a simple class that contains the two strings as properties and return that. EDIT: or use `out` or `ref` parameters, but that may be a tad painful to use. – Chris Sinclair May 09 '14 at 14:26

6 Answers6

12

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

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);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kilazur
  • 3,089
  • 1
  • 22
  • 48
0

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;

}

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ricardo Romo
  • 1,588
  • 12
  • 25
0

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";
    }
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41