I have a function where I want to return two values. Is this possible?
This is my code, but it doesn't seem to like that I want to return two values:
public string PlayerCards(string player1C1, string player1C2)
{
generatedCard = randomCard.Next(1, 52);
player1C1 = generatedCard.ToString();
player1C1 = player1C1 + ".png";
return player1C1, player1C2;
}
In C or C++, we cannot return multiple values from a function directly. In this section, we will see how to use some trick to return more than one value from a function. We can return more than one values from a function by using the method called “call by address”, or “call by reference”.
No, you can not have two returns in a function, the first return will exit the function you will need to create an object.
Return multiple values using commas In Python, you can return multiple values by simply return them separated by commas. In Python, comma-separated values are considered tuples without parentheses, except where required by syntax.
A function can not return multiple values, but similar results can be obtained by returning an array.
You can return tuple: Tuple<string, string>
Tuple<string, string> t = new Tuple<string, string>(player1C1,player1C2);
return t;
Some options:
Use an out
parameter:
public string PlayerCards(out string x)
Return one value, and set the out
parameter (x
in this case) to another value; the calling code will need to specify an argument with out
as well, and after the call has completed, the caller will be able to see the value set in the method.
(It's not clear why you're accepting parameters at all; you don't seem to really use them.)
Return a ValueTuple<string, string>
, ideally using C# 7 tuples to provide element names
Tuple<string, string>
PlayingCard
type.It's not at all clear what your code is trying to do - the name of the method isn't clear and you don't use the parameters. When you've clarified what the method is trying to achieve - to yourself as much as to us - the answer may well become more obvious.
I'd also encourage you to use local variables where appropriate - I suspect generatedCard
should be a local variable instead of the (presumably) instance variable it currently is.
I think that you can use string array...
Second way is to use a struct containing two string values or a class with two string member,,
Look at here:
/// <summary>
/// Using struct
/// </summary>
struct twoStringValue
{
public string s1, s2;
}
public twoStringValue PlayerCards(string player1C1, string player1C2)
{
twoStringValue tsv;
generatedCard = randomCard.Next(1, 52);
tsv.s1 = player1C1 = generatedCard.ToString();
tsv.s1 = player1C1 = player1C1 + ".png";
return tsv;
}
/// <summary>
/// Using a class
/// </summary>
class TwoStringValue
{
public string str1;
public string str2;
}
public TwoStringValue PlayerCards(string player1C1, string player1C2)
{
TwoStringValue tsv;
generatedCard = randomCard.Next(1, 52);
tsv.str1 = player1C1 = generatedCard.ToString();
tsv.str1 = player1C1 = player1C1 + ".png";
return tsv;
}
Good Luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With