11

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin
  • 1,435
  • 4
  • 19
  • 27
  • Will this always only return two card image names? Or do you want it to be more general, so it could return more than two? If so, you could just return a `List`. I'd also consider renaming the method to `PlayerCardImageFileNames()` – Matthew Watson May 12 '13 at 08:47
  • This question has been asked just few days ago.http://stackoverflow.com/questions/16411685/return-multiple-values-from-a-class-to-method – FeliceM May 12 '13 at 08:48

4 Answers4

19

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

  • Return a Tuple<string, string>
  • Create a new type to store the two values together, assuming it's a meaningful combination. This is definitely a good choice if the values are related in a way which you'll use elsewhere. For example, instead of having a method returning one string for the suit of a card and one for the value, you'd create a PlayingCard type.
  • Refactor your code into two method calls, each of which return a single value

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    How did you answer this thoroughly in 2 minutes?! "Jon Skeet is so good, he's written a program to auto-answer questions on SO for him." – Devin May 12 '13 at 07:16
  • 1
    JonSkeet doesn't write answers..He writes programs and code in SO...The output is in numbers of upvotes... – S2S2 May 12 '13 at 07:25
  • 1
    @VijayS - I so wish I could come with a Jon Skeet Soviet Russia parody joke. Something like "...In Jon Skeet's mind code writes itself" – Security Hound May 12 '13 at 08:31
14

You can return tuple: Tuple<string, string>

Tuple<string, string> t = new Tuple<string, string>(player1C1,player1C2);

return t;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fatihk
  • 7,789
  • 1
  • 26
  • 48
4

One of the several possible options:

Create a struct like this:

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

Then use it in your function like this:

public Players PlayerCards()
    {   
        Players p1;
        generatedCard = randomCard.Next(1, 52);
        p1.Player1 = generatedCard.ToString();
        p1.Player2 =  p1.Player1 + ".png";            
        return p1;
    }
Amit Mittal
  • 1,129
  • 11
  • 30
2

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.

Mohsen Bahman
  • 1,092
  • 3
  • 15
  • 27