Solved

C# - Refactor Code

Posted on 2014-09-24
1
108 Views
Last Modified: 2014-09-25
Can the below code be refactored?

    private void PayrollDeduction(DataSet myDataSet)
    {
        DataTable dtPreTax = new DataTable();
        DataTable dtPostTax = new DataTable();
        double dblPreTaxTotal = 0;
        double dbloPreTaxTotal = 0;
        double dblPostTaxTotal = 0;

        //Get PreTax Data
        dtPreTax = myDataSet.Tables["Deduction"].Copy();
        dtPreTax.TableName = "dtPreTax";
        DataRow[] drPreTax = dtPreTax.Select("postTax=true");
        for (int i = 0; i < drPreTax.Length; i++)
            drPreTax[i].Delete();
        dtPreTax.AcceptChanges();

        DataColumn col = new DataColumn("Sort", typeof(System.Int16));
        col.DefaultValue = 0;
        dtPreTax.Columns.Add(col);

        if (dtPreTax != null)
        {
            //Get Total
            dblPreTaxTotal = Convert.ToDouble(dtPreTax.Compute("Sum(amount)", ""));
            if (dblPreTaxTotal >= 0)
                dbloPreTaxTotal = dblPreTaxTotal;

            //Decide Description
            if (dblPreTaxTotal > 0)
                this.lblPreTaxDesc.Text = "Total Deduction (PreTax)";
            else
                this.lblPreTaxDesc.Text = "Total Credit";
            double dblPreTxTotal = 0;
            //Do not display Negative sign
            if (dblPreTaxTotal <= 0)
                dblPreTxTotal = Math.Abs(dblPreTaxTotal); 
            //Display PreTax Total
            if (dblPreTaxTotal < 0)
                this.lblPreTaxAmt.Text = dblPreTxTotal.ToString("c");
            else
                this.lblPreTaxAmt.Text = dbloPreTaxTotal.ToString("c");
        }

        //Do not display negative values on GUI
        foreach (DataRow row in dtPreTax.Rows)
        {
            if (row[0].ToString().Contains("Budgeted"))
            {
                //Set Sort
                row["Sort"] = 1;
            }
            if (Convert.ToDouble(row[1].ToString()) < 0)
            {
                //Convert negative to positive
                row["amount"] = Convert.ToDouble(row[1].ToString()) * -1;
            }
        }
        dtPreTax.AcceptChanges();

        //Sort
        dtPreTax.DefaultView.Sort = "Sort ASC";

        //Bind PreTax Data
        this.rptPreTax.DataSource = dtPreTax;
        this.rptPreTax.DataBind();

        //Get PostTax Data
        dtPostTax = myDataSet.Tables["Deduction"].Copy();
        dtPostTax.TableName = "dtPostTax";
        DataRow[] drPostTax = dtPostTax.Select("postTax=false");
        for (int i = 0; i < drPostTax.Length; i++)
            drPostTax[i].Delete();
        dtPostTax.AcceptChanges();

        //Does PostTax Data Exist?
        if (dtPostTax.Rows.Count == 0 || dtPostTax == null)
        {
            this.pnlPostTax.Visible = false;
        }
        else
        {
            dblPostTaxTotal = Convert.ToDouble(dtPostTax.Compute("Sum(amount)", "")); ;
            this.lblPostTaxAmt.Text = dblPostTaxTotal.ToString("c");
            this.lblPostTaxDesc.Text = "Total Payroll Deduction (PostTax)";

            this.pnlPostTax.Visible = true;
            this.rptPostTax.DataSource = dtPostTax;
            this.rptPostTax.DataBind();
        }

        this.lblGrandTotal.Text = "Deduction Grand Total";

        double dblGrandTotal = dbloPreTaxTotal + dblPostTaxTotal;
        if (dblGrandTotal < 0)
            dblGrandTotal = 0;
        this.lblGrandTotalAmt.Text = dblGrandTotal.ToString("c");
    }

Open in new window

0
Comment
Question by:CipherIS
1 Comment
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40342182
yes.

There's no need for extra variables.


//just display the absolute value regardless
  this.lblPreTaxAmt.Text = math.Abs(dbloPreTaxTotal).ToString("c");

replaces
            double dblPreTxTotal = 0;
            //Do not display Negative sign
            if (dblPreTaxTotal <= 0)
                dblPreTxTotal = Math.Abs(dblPreTaxTotal); 
            //Display PreTax Total
            if (dblPreTaxTotal < 0)
                this.lblPreTaxAmt.Text = dblPreTxTotal.ToString("c");
            else
                this.lblPreTaxAmt.Text = dbloPreTaxTotal.ToString("c");

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wav problem 4 27
Where did System.Data.Objects go? 2 28
How to make a Gridview column hidden (visibility false) in the HTML code? 2 16
What Does This C# Code Block Do? 5 32
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question