I'm pretty new to coding and I'm creating a quiz using C#, however i'm running into problems when trying to increment the total score when an answer is answered correctly. the code calling the method is:
private void nextQuestionButton1_Click(object sender, EventArgs e) { if (majNugentRadioButton.Checked) { // increment total score Program.Score(); // load question 2 Question2 win2 = new Question2(); win2.Show(); this.Close(); } else { // load question 2 Question2 win2 = new Question2(); win2.Show(); this.Close(); } } } And the code for the Program.Score(); method is:
static public void Score() { int totalScore = 0; totalScore++; } When i call this method from the 2nd question it sets the totalScore back to 0, how can i get it so that it only assigns the value of 0 the first time it's called?
totalScoreis locally scoped to theScore()method which means it only exists within this method. Basically it creates a new variable, assignes it a value of 0, increments it by one, then discards the variable.