'C# Add numbers based on checkboxes checked [closed]

I'm trying to add numbers together based on check boxes checked in a form. The Extras sum doesn't add these correctly. For example, if I choose checkBox1 and checkBox2 the total shows 1 and not 21. Could you please assist? See the code below. Thanks

Double Extras = 0;
        if(checkBox1.Checked ==true)
        {
            Extras = +1;
        }
        if (checkBox2.Checked ==true)
        {
            Extras = +20.0;
        }
        if (checkBox3.Checked ==true)
        {
            Extras = +20.0;
        }
        if (checkBox4.Checked ==true)
        {
            Extras = +2.0;


Solution 1:[1]

You must use +=, This operator is one of the basic c# topics and is equivalent: Extras = Extras + N;

Double Extras = 0;
        
if (checkBox1.Checked) Extras += 1;
    
if (checkBox2.Checked) Extras += 20;

Solution 2:[2]

Double Extras = 0;
if(checkBox1.Checked)
    Extras += 1;
if (checkBox2.Checked)
    Extras += 20.0;
if (checkBox3.Checked)
    Extras += 20.0;
if (checkBox4.Checked)
    Extras += 2.0;

Solution 3:[3]

Rewrite the code as follows and you can get what you want:

Double Extras = 0;
if(checkBox1.Checked)
    Extras += 1;
if (checkBox2.Checked)
    Extras += 20.0;
if (checkBox3.Checked)
    Extras += 20.0;
if (checkBox4.Checked)
    Extras += 2.0;

Your code:

Extras = +1;

sets the variable to 1, NOT ADDING 1(number) to the variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KiynL
Solution 2 Prasad Telkikar
Solution 3