2

Refer to method WutHap (sorry, I didn't write comments), I need to return both int agg and int sub to get the method NumGen would have on int agg and sub in Main. I know you can use arrays and tuples, but I'm having trouble doing it. If you need any more context, please ask what needs to be clarified. I'm stupid, so saying that it needs more context will probably not lead to actual clarification. Yes, I know the code is abysmal but that isn't the focus.

class Program
    {
        static void Main(string[] args)
        {
            int agg = 50;
            int sub = 50;
            for (int i = 0; i < 100; i++)
            {
                WutHap(agg, sub);
            }
            int pop = sub + agg;
            Console.WriteLine("Total aggs = " + agg);
            Console.WriteLine("Total subs = " + sub);
            Console.WriteLine("Total population = " + pop);
            Console.ReadKey();
        }
        static string NumGen()
        {
            string[] pengs = new string[2]{"agg", "sub"};
            Random numGen = new Random();
            int mepi = numGen.Next(1, 2);
            int mepi2 = numGen.Next(1, 2);
            string pemi = pengs[mepi];
            string pemi2 = pengs[mepi2];
            string whoMet = pemi + ", " + pemi2;
            return whoMet;
        }
        static int WutHap(int agg, int sub)
        {
            NumGen();
            switch (NumGen())
            {
                case ("agg, agg"):
                    --agg;
                    --agg;
                    break;
                case ("agg, sub"):
                    ++agg;
                    ++agg;
                    ++agg;
                    --sub;
                    break;
                case ("sub, agg"):
                    --sub;
                    ++agg;
                    ++agg;
                    ++agg;
                    break;
                case ("sub, sub"):
                    ++sub;
                    ++sub;
                    break;
            }
        }

    }
Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34
Rattus Rattus
  • 21
  • 1
  • 1
  • 3
  • `I know you can use arrays and tuples, but I'm having trouble doing it.` touples should be fine for your case, what trouble do you have using touples? – Longoon12000 Dec 16 '19 at 12:00
  • I actually just tried arrays and haven't worked tuples before. I will try it. – Rattus Rattus Dec 16 '19 at 12:01
  • You could: use C#'s ref/out parameters, return a tuple or make a small class. – Carra Dec 16 '19 at 12:03
  • You probably could also pass the variables as reference and change them directly (like Carra suggests) without having to return anything. Also, why are you executing `NumGen()` twice in a row? – Longoon12000 Dec 16 '19 at 12:03
  • I didn't know I was executing twice. `NumGen(); switch (NumGen()` You mean there? I thought you had to just execute it in the method so you can call it. – Rattus Rattus Dec 16 '19 at 12:08

1 Answers1

8

You can use C# 7 syntax to return multiple values from method:

static void Main(string[] args)
{
    var (a, b) = Foo();
}


private static (int, int) Foo()
{
    return (1, 2);
}

UPDATE:

Using Tuple<int, int>:

static void Main(string[] args)
{
    var (a, b) = GetMultipleValue();
    Tuple<int, int> tuple = GetMultipleValue();
}

public static Tuple<int, int> GetMultipleValue()
{
    return Tuple.Create(1, 2);
}
StepUp
  • 36,391
  • 15
  • 88
  • 148