Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
every time a player wins i would like for winner to keep track, but it wont keep increment the numofwins variable.
And also every cash variable wont decrement while you play the game

else
                {


                    cash = cash - 1;
                    num1 = RndNumber.Next(0, 7);
                    num2 = RndNumber.Next(0, 7);
                    num3 = RndNumber.Next(0, 7);

                    btnOne.Text = num1.ToString();
                    btnTwo.Text = num2.ToString();
                    btnThree.Text = num3.ToString();

                    if ((btnOne.Text == btnTwo.Text) && (btnTwo.Text == btnThree.Text))
                    {
                        cash++;
                        numofWins++;
                        textBox1.Text = numofWins.ToString();
                        MessageBox.Show("Congrats! you win");
                        return;
                    }
                }


What I have tried:

i try debugging and i see the variable change but it wont update in textbox or label box.
Posted
Updated 16-May-23 6:10am

1 solution

It's going to depend on where nomofWins is declared:
1) If it is a local variable - i.e. defined within the method body - then since the method exits immediately after incrementing it the variable will be discarded and recreated when the method is next called. The value cannot be preserved once it goes out of scope.
2) If it is a parameter to the method, then it is by default passed by value, which means that any changes made to it inside the method are not reflected back into the original variable that was passed.

There are ways to fix this, but the depend on a lot of factors - including the environment in which the code is executed: a web based solution would be different to a WinForms one for example.

So start out by finding out where it is declared, and work out what the variable lifetime is. Then you can think about fixing it to preserve the value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900