Objective: User enters a score then clicks Add button. Everytime Add button is clicked a Score is added to previous score to display total, averaged,counter.

Issue: I enter first scores then click add. The first score displays but the second score overwrites the first. It is not accumulating.

I believe I am missing something fundamental or should I create a seperate method to keep the running total and count. Here is a snippet of what I have

private void  AddScore_Click(object sender, EventArgs e)
        {
            decimal Score=0m,       
                     Average=0m,
                     ScoreTotal=0m;        
             
            List<decimal> myScoreList = new  List<decimal>();

            myScoreList.Add(Convert.ToDecimal(Score.Text));
               
               //myScoreList.Add(5); 
               //myScoreList.Add(6);
              //myScoreList.Add(7);

            for (int i = 0; i < myScoreList.Count; i++)
            {
                 
                 Score = myScoreList [i];
                 ScoreTotal +=  Score;
                 Average =  ScoreTotal / myScoreList.Count;                       
                
            }
             
            this.lblDisplayScrTotal.Text =  ScoreTotal.ToString();
            this.lblDisplayScrCount.Text = ScoreList.Count.ToString();
            this.lblDisplayAverage.Text =  Average.ToString();
        }
         
    }        }

every time you call this function each member(Score,Average,ScoreTotal,myScoreList) will be cleared (as you start over) to avoid this declare variables you need to keep its values out side the AddScore_Click function or declare as static

every time you call this function each member(Score,Average,ScoreTotal,myScoreList) will be cleared (as you start over) to avoid this declare variables you need to keep its values out side the AddScore_Click function or declare as static

Thank you for your input. I cant change the method to static since it is button_click event handler generated. I did take my variables and declared them outside the method. but now it is adding the number in twice. for example.
I enter 10 click addscore. I add 10 again click add and now the total is 30. Could this be my for loop?

mazzica1
Thank you for the help after running a debug I was able to identify the issue and solve calculations issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.