adding scores from a list

input a score into a text box, then put another score to get totalScor

Page 1 of 1

7 Replies - 2431 Views - Last Post: 18 June 2010 - 12:49 PM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

adding scores from a list

Posted 17 June 2010 - 06:07 PM

I am using .net framework in visual studio 2008 C#
I have a form that I am to input a score, there is a score text box that displays the score so far. The scores go into a list, and the list is working fine, but what I am having a problem with is every time I insert a new score the scoreTotal matches the amount, it is not adding the previous score to the new score. What am I doing wrong.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project_8_1
{
    public partial class FrmScoreCalculator : Form
    {
        public FrmScoreCalculator()
        {
            InitializeComponent();
        }

        List<int> scoreList = new List<int>();

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int score = Convert.ToInt32(txtScore.Text);
            
            int scoreTotal =+ score;

            //add score to list
            scoreList.Add(score);
            
            txtScoreTotal.Text = scoreTotal.ToString();
            txtScore.Focus();
        }

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {            
           scoreList.Sort();
           string message = "";
            foreach (int score in scoreList)
            {
                message += score.ToString() + "\n";
            }
            MessageBox.Show(message, "Sorted Scores");
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: adding scores from a list

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: adding scores from a list

Posted 17 June 2010 - 07:34 PM

int scoreTotal =+ score;

This is the only use of scoreTotal I can find, and it goes out of scope once the method btnAdd_Click() is done. This isn't tracking the total. One way to fix it is move it so it is part of the class and not part of the method:
public partial class FrmScoreCalculator : Form {
    int scoreTotal = 0;

    ... my methods ...

    scoreTotal += score;

    ... rest of code ...
}

Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: adding scores from a list

Posted 17 June 2010 - 08:20 PM

That didnt help, I know it has to do with looking up the Key and Value in the List and calculating the scoreTotal off of the Value for that Key
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: adding scores from a list

Posted 17 June 2010 - 08:27 PM

I can't imagine it didn't help at all...what you were originally doing was declaring an int (which is initialized to zero), then adding the score to it. Therefore, it would always be equal to the score that was just entered. If you make the total variable a class-level variable, it would persist the previous value.

Anyway, there's a much simpler way to do this:
int score = scoreList.Sum();


This way, you never have to keep a running score. It doesn't matter if you add, remove, or change values, that will always give you the sum of the List.

This post has been edited by insertAlias: 17 June 2010 - 08:29 PM

Was This Post Helpful? 1
  • +
  • -

#5 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: adding scores from a list

Posted 18 June 2010 - 12:28 AM

namespace Project_8_1
{
    public partial class Form1 : Form
    {
        private List<int> scores = new List<int>();
        private int total_score = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int score = Convert.ToInt32(txtScore.Text);
            scores.Add(score);

            CalcTotalScore();
            DisplayScore();
        }

        private void CalcTotalScore()
        {
            total_score = scores.Sum();
        }

        private void DisplayScore()
        {
            txtScoreTotal.Text = total_score.ToString();
            txtScore.Focus();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: adding scores from a list

Posted 18 June 2010 - 09:56 AM

Thank you, it was the score.Sum() I needed, I couldn't find that anywhere. Thank you all for your help
Was This Post Helpful? 0
  • +
  • -

#7 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: adding scores from a list

Posted 18 June 2010 - 11:59 AM

I'd just like to point out that in this situation, Sum() is an ineffecient solution as it has to add all the values every time you enter one. OTOH doing a running sum only does one addition each time. This isn't likely to be an issue as I doubt you will be entering millions of values, but if you ever need to read in some data, do some processing and keep a sum, keep this in mind :)
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4531
  • View blog
  • Posts: 7,902
  • Joined: 08-June 10

Re: adding scores from a list

Posted 18 June 2010 - 12:49 PM

True, all sum really does is loop through each element, add it to a running total, and return the result, so it's an O(n) operation. But I figured it'd be efficient enough for this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1