mercredi 30 mars 2016

C# Loop checkbox and add value

I'm trying to add values of phones, where if a checkbox1 (which represpent phone1) is checked and checkbox2 is also checked. The program will add both phones' values. How to add the value in for loop so that the if statement is lesser and simplified.

public double totalPhone()
    {
        double total = 0;
        double item1 = 2249;
        double item2 = 1769;
        double item3 = 3099;
        double item4 = 1198;
        double item5 = 1899;

        if (chkPhone1.Checked == true)
        {
            total = total + item1;
        }

        if (chkPhone2.Checked == true)
        {
            total = total + item2;
        }

        if (chkPhone3.Checked == true)
        {
            total = total + item3;
        }

        if (chkPhone4.Checked == true)
        {
            total = total + item4;
        }

        if (chkPhone5.Checked == true)
        {
            total = total + item5;
        }

        return total;
    }




Aucun commentaire:

Enregistrer un commentaire