Returning two strings in a function in C#

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.


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

Tags:

C#

Return