1

I'm trying to make a counter that will increment based on the response of the user. Here's the code I got so far:

        string ok = "";
        int z = 0;
        test(ok, z);
        test1(ok, z);
        Console.WriteLine(z);
    }

        static void test(string ok, int z)
        {

            bool estok = false;
            while (!estok)
            {
                ConsoleKeyInfo saisie = Console.ReadKey(true);
                if (saisie.Key == ConsoleKey.A || saisie.Key == ConsoleKey.B)
                {
                    estok = true;
                    if (saisie.Key == ConsoleKey.A)
                    {

                        z++;
                    }

                    if (saisie.Key == ConsoleKey.B)
                    {
                        z--;
                    }
                }
                else
                {
                    estok = false;
                    Console.WriteLine("Error");
                }
            }


        }
            static void test1(string ok, int z)
            {
                bool estok = false;
                while (!estok)
                {
                    ConsoleKeyInfo saisie = Console.ReadKey(true);
                    if (saisie.Key == ConsoleKey.A || saisie.Key == ConsoleKey.B)
                    {
                        estok = true;
                        if (saisie.Key == ConsoleKey.A)
                        {
                            z++;
                        }

                        if (saisie.Key == ConsoleKey.B)
                        {
                            z--;
                        }
                    }
                    else
                    {
                        estok = false;
                        Console.WriteLine("Error");
                    }
                }
            }

I got 2 functions (test and test1) that both increment the int z. The Console.WriteLine(z) will return me 0, in place of the 2 i'm waiting for (when the user has 2 correct answers).

I figured that the increment won't happen since it's in the function and that the Console.WriteLine(z) can't reach the z++. How can I change that ?

How can I get the result from these?

2
  • "How to debug small programs"
    – Uwe Keim
    Commented May 5, 2020 at 5:40
  • 1
    C# method parameters have value semantics by default. Changes you do in the method to not apply to the object you passed from Main(). The better method here is to return the value z from the methods.
    – Lennart
    Commented May 5, 2020 at 5:40

2 Answers 2

0

int and other primitive types are passed by value by default where as reference types (think instances of a class) are passed by reference; this is what allows changes to the parameter to be persisted after the method returns. The way you are updating the value of the parameter, you'll need to pass z by reference.

static void test(string ok, int z) becomes static void test(string ok, ref int z)

and the call test(ok, z); becomes test(ok, ref z);

You can learn more about passing values by reference from the C# Language Reference

0

method parameters for int are of value types and not reference type, as far as I understood from your question, you may need to use out keyword in the method call or return from the method you have.

int z1= z;
test(ok, out z1);
int z2=z;
test1(ok, out z2);

and the method declaration also will have to be changed to

static void test(string ok, out int z)


static void test1(string ok, out int z)

OR you can simply put a Console.WriteLine(z) in the method test and test1 directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.