Search code examples
c#subtraction

Take the sum of a list as variable and edit it at runtime


I'm trying to calculate the end value of a specific variable that depends whether a checkbox is checked. This is however dynamically done.

What I'm trying to achieve:

enter image description here

The example above works fine but it starts going wrong when I start to uncheck the check boxes. It subtracts by the double amount of the time and I have no idea why:

enter image description here

My Code (TimeSpent is the variable that I want to have at the end):

Class

public class Activity
{
    public int ID { get; set; }
    public int IncidentID { get; set; }
    public string Description { get; set; }
    public int TimeSpent { get; set; }
    public static int StartX { get; set; } = 10;
    public static int StartY { get; set; } = 10;

    private TextBox textBox = null;

    private CheckBox checkBox = null;

    public Activity(int iD, int incidentID, string description, int timeSpent)
    {
        this.ID = iD;
        this.IncidentID = incidentID;
        this.Description = description;
        this.TimeSpent = timeSpent;
    }

    public Activity()
    { }

    public bool isChecked() {
        return checkBox.Checked;
    }

    public void setTimeSpent(int timeSpent) {
        this.TimeSpent = timeSpent;
        textBox.Text = timeSpent.ToString();
    }

    public int getTimeSpent()
    {
        return Int32.Parse(textBox.Text);
    }

    public void DrawToForm(Panel p)
    {    
        var label = new Label();
        textBox = new TextBox();
        checkBox = new CheckBox();
        checkBox.Checked = true;
        textBox.Size = new System.Drawing.Size(40, 20);
        label.Text = Description.ToString();
        label.AutoSize = true;
        textBox.Text = TimeSpent.ToString();
        label.Left = StartX;
        label.Top = StartY;
        StartX += 430;// Move position to right
        textBox.Left = StartX;
        textBox.Top = StartY;
        StartX += 160;// Move position to right
        checkBox.Left = StartX;
        checkBox.Top = StartY;
        StartX = 10;// Reset to start
        StartY += 50;// Move position to down
        p.Controls.Add(label);
        p.Controls.Add(textBox);
        p.Controls.Add(checkBox);
    }
}

GUI:

private void Btn_Validate_Click(object sender, EventArgs e)
{      
    tb_TotalTime.Text = calculateTotalTime().ToString();
}

public int calculateTotalTime()
{
    int total = 0;
    foreach (Activity a in activities)
    {                  
        if(a.isChecked())
        {
            total += a.getTimeSpent();
        }else
        {
            total -= a.getTimeSpent();
        }
    }
    return total;
}

Why is subtracting not done correctly?


Solution

  • You subtract when the checkbox is not checked:

    if(a.isChecked())
    {
        total += a.getTimeSpent();
    }
    else
    {
        total -= a.getTimeSpent();// << THIS
    }
    

    So with the setup of your 2nd image (top 2 unchecked bottom one checked) you do this:

    0 - 5 - 5 + 5 = -5
    

    The 0 comes from the default value of Total (int)


    To fix this you could just remove the else clause as you don't need to subtract billable time ever.