user3542214
user3542214

Reputation: 23

C#, value getting over-written instead of retaining old value and adding new

I am trying to make it so that the value of total per is the sum of all the employeePer*, For example if there are two employees with employeePer1= 2 and employeePer2 = 2, the total should be 4, entering the first two if statements. But instead totalPer is always assigned the value of the last if statement it went into e.g in the example above totalper would == 2 not 4, Thanks for any help

protected void Page_Load(object sender, EventArgs e)
{
    int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
    int totalPercent = 100*(Convert.ToInt32(Request.QueryString["employees"]));
    Response.Write(totalPercent);
    int totalPer = 0;

    if(numberOfEmps == 1)
    {
    int employee1Per =  (Convert.ToInt32(Request.QueryString["employee1Per"]));
    Response.Write(employee1Per);
    int employee1Att = (Convert.ToInt32(Request.QueryString["employee1Att"]));
    int employee1Sal = (Convert.ToInt32(Request.QueryString["employee1Sal"]));
    int employee1Yer = (Convert.ToInt32(Request.QueryString["employee1Yer"]));
        totalPer += employee1Per;
    }
    if(numberOfEmps == 2)
    {
    int employee2Per = (Convert.ToInt32(Request.QueryString["employee2Per"]));
    int employee2Att = (Convert.ToInt32(Request.QueryString["employee2Att"]));
    int employee2Sal = (Convert.ToInt32(Request.QueryString["employee2Sal"]));
    int employee2Yer = (Convert.ToInt32(Request.QueryString["employee2Yer"]));
        totalPer +=  employee2Per;
    }
    if(numberOfEmps == 3)
    {
    int employee3Per = (Convert.ToInt32(Request.QueryString["employee3Per"]));
    int employee3Att = (Convert.ToInt32(Request.QueryString["employee3Att"]));
    int employee3Sal = (Convert.ToInt32(Request.QueryString["employee3Sal"]));
    int employee3Yer = (Convert.ToInt32(Request.QueryString["employee3Yer"]));
        totalPer +=employee3Per;
    }
    if(numberOfEmps == 4)
    {
    int employee4Per = (Convert.ToInt32(Request.QueryString["employee4Per"]));
    int employee4Att = (Convert.ToInt32(Request.QueryString["employee4Att"]));
    int employee4Sal = (Convert.ToInt32(Request.QueryString["employee4Sal"]));
    int employee4Yer = (Convert.ToInt32(Request.QueryString["employee4Yer"]));
        totalPer +=employee4Per;
    }
    if(numberOfEmps == 5)
    {
    int employee5Per = (Convert.ToInt32(Request.QueryString["employee5Per"]));
    int employee5Att = (Convert.ToInt32(Request.QueryString["employee5Att"]));
    int employee5Sal = (Convert.ToInt32(Request.QueryString["employee5Sal"]));
    int employee5Yer = (Convert.ToInt32(Request.QueryString["employee5Yer"]));
        totalPer +=employee5Per;
    }
    if(numberOfEmps == 6)
    {
    int employee6Per = (Convert.ToInt32(Request.QueryString["employee6Per"]));
    int employee6Att = (Convert.ToInt32(Request.QueryString["employee6Att"]));
    int employee6Sal = (Convert.ToInt32(Request.QueryString["employee6Sal"]));
    int employee6Yer = (Convert.ToInt32(Request.QueryString["employee6Yer"]));
        totalPer +=employee6Per;
    }
    if(numberOfEmps == 7)
    {
    int employee7Per = (Convert.ToInt32(Request.QueryString["employee7Per"]));
    int employee7Att = (Convert.ToInt32(Request.QueryString["employee7Att"]));
    int employee7Sal = (Convert.ToInt32(Request.QueryString["employee7Sal"]));
    int employee7Yer = (Convert.ToInt32(Request.QueryString["employee7Yer"]));
        totalPer +=employee7Per;
    }
    if(numberOfEmps == 8)
    {
    int employee8Per = (Convert.ToInt32(Request.QueryString["employee8Per"]));
    int employee8Att = (Convert.ToInt32(Request.QueryString["employee8Att"]));
    int employee8Sal = (Convert.ToInt32(Request.QueryString["employee8Sal"]));
    int employee8Yer = (Convert.ToInt32(Request.QueryString["employee8Yer"]));
        totalPer +=employee8Per;
    }
    if(numberOfEmps == 9)
    {
    int employee9Per = (Convert.ToInt32(Request.QueryString["employee9Per"]));
    int employee9Att = (Convert.ToInt32(Request.QueryString["employee9Att"]));
    int employee9Sal = (Convert.ToInt32(Request.QueryString["employee9Sal"]));
    int employee9Yer = (Convert.ToInt32(Request.QueryString["employee9Yer"]));
        totalPer +=employee9Per;
    }
    if(numberOfEmps == 10)
    {
    int employee10Per = (Convert.ToInt32(Request.QueryString["employee10Per"]));
    int employee10Att = (Convert.ToInt32(Request.QueryString["employee10Att"]));
    int employee10Sal = (Convert.ToInt32(Request.QueryString["employee10Sal"]));
    int employee10Yer = (Convert.ToInt32(Request.QueryString["employee10Yer"]));
        totalPer +=employee10Per;
    }
    if(numberOfEmps == 11)
    {
    int employee11Per = (Convert.ToInt32(Request.QueryString["employee11Per"]));
    int employee11Att = (Convert.ToInt32(Request.QueryString["employee11Att"]));
    int employee11Sal = (Convert.ToInt32(Request.QueryString["employee11Sal"]));
    int employee11Yer = (Convert.ToInt32(Request.QueryString["employee11Yer"]));
        totalPer +=employee11Per;
    }
    if(numberOfEmps == 12)
    {
    int employee12Per = (Convert.ToInt32(Request.QueryString["employee12Per"]));
    int employee12Att = (Convert.ToInt32(Request.QueryString["employee12Att"]));
    int employee12Sal = (Convert.ToInt32(Request.QueryString["employee12Sal"]));
    int employee12Yer = (Convert.ToInt32(Request.QueryString["employee12Yer"]));
        totalPer +=employee12Per;
    }

    Label7.Text = (totalPer.ToString());
    Label9.Text = (totalPercent.ToString());

}

Upvotes: 0

Views: 87

Answers (3)

Paul Aldred-Bann
Paul Aldred-Bann

Reputation: 6020

There's a lot of refactoring you could do here, and although this question has been answered correctly I felt you might benefit from another perspective to achieve what you're trying to do.

First, I'd have some kind of structure to store my employees in, so let's use a struct for example:

struct Employee
{
    int Per;
    int Att;
    int Sal;
    int Yer;
}

Now, because the number of employees is a variable, what you have doesn't scale past 12 so I'd for through to the number given and create instances of Employee from the QueryString data and store them in some kind of collection List<T> for now, something like this:

List<Employee> employees = new List<Employee>();    
int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
for (int i = 1; i <= numberOfEmps; i++)
{
    Employee employee = new Employee()
    {
        Per =  Convert.ToInt32(Request.QueryString[string.Format("employee{0}Per", i)]),
        Att = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Att", i)]),
        Sal = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Sal", i)]),
        Yer = Convert.ToInt32(Request.QueryString[string.Format("employee{0}Yer", i)])
    };
    employees.Add(employee);
}

Then you have 2 options of counting up your Per properties, you could use an iterator:

int totalPer = 0;
foreach (var employee in employees)
{
    totalPer = totalPer + employee.Per;
}

Or you could use LINQ:

totalPer = employees.Sum(e => e.Per);

Upvotes: 1

Jon P
Jon P

Reputation: 19797

You could make this a whole lot simpler using iteration as it appears you aren't doing much with employee[x]Per, employee[x]Att and employee[x]Sal

Something like:

int numberOfEmps = Convert.ToInt32(Request.QueryString["employees"]);
int totalPercent = 100*(Convert.ToInt32(Request.QueryString["employees"]));
Response.Write(totalPercent);
int totalPer = 0;

for(int i = 1; i <= numberOfEmps; i++)
{
   int employeePer = (Convert.ToInt32(Request.QueryString["employee" + i + "Per"]));
   totalPer +=employeePer;
}

Upvotes: 0

Guffa
Guffa

Reputation: 700860

If there are two employees, the code won't enter the first two if statemenets, it will only enter the second.

For the first if statement you would want to check if there are one employee or more, not exactly one employee:

if(numberOfEmps >= 1)

Then the corresponding for all the other if statements.

Upvotes: 7

Related Questions