0

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();
        }
         
    }        }
2
Contributors
3
Replies
7
Views
6 Years
Discussion Span
Last Post by happyhound
0

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

0

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?

Edited by happyhound: New information since last post

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.