PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How to make a counter based on user response?

 July 06, 2022     c#, counter, pass-by-reference     No comments   

Issue

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?


Solution

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



Answered By - Babak Naffas
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing