Returns 2 rows with a function in C #

I have a function where I want to return 2 values? Is it possible?

This is my code, but it seems to me that I do not want to return 2 values

public string PlayerCards(string player1C1, string player1C2) { generatedCard = randomCard.Next(1, 52); player1C1 = generatedCard.ToString(); player1C1 = player1C1 + ".png"; return player1C1, player1C2; } 

I did some searches, but did not find what I needed.

+7
source share
4 answers

Some options:

  • Use the 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 , and after the call is completed, the caller can see the value set in the method.

    (It’s not clear why you are accepting parameters at all, you are apparently not using them.)

  • Return a Tuple<string, string>

  • Create a new type to store the two values ​​together, assuming this is a meaningful combination. This is definitely a good choice if the values ​​are related in a way that you will use elsewhere. For example, instead of a method returning one row for a card suit and another for a value, you should create a type PlayingCard .
  • Code refactoring into two method calls, each of which returns a single value

It is not clear what your code is trying to do - the method name is not clear, and you are not using parameters. When you clarify what the method is trying to achieve, for you, as for us, the answer may become more obvious.

I also recommend that you use local variables if necessary - I suspect that generatedCard should be a local variable instead of the variable (presumably) of the instance that it is currently.

+14
source

you can return a tuple: Tuple<string, string>

 Tuple<string, string> t = new Tuple<string, string>(player1C1,player1C2); return t; 
+10
source

One of several options:

Create a structure like this:

 struct Players { public string Player1; public string Player2; } 

Then use it in your function as follows:

 public Players PlayerCards() { Players p1; generatedCard = randomCard.Next(1, 52); p1.Player1 = generatedCard.ToString(); p1.Player2 = p1.Player1 + ".png"; return p1; } 
+4
source

I think you can use a string array ...

The second way is to use a structure containing two string values ​​or a class with two string members,

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.

+2
source

All Articles