我有一个方法,其中包含14个if语句,而我又要做12次完全相同的操作,例如160次if语句。我如何重构以使其更清洁?我正在使用telerik radgrid,并且将条件格式应用于单元格,但是对于每一列它都不同,并且取决于不同列中的值而不同。这是我方法的开始。

菲:确实有效。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        //Is it a GridDataItem
        if (e.Item is GridDataItem)
        {
            //Get the instance of the right type
            GridDataItem dataBoundItem = e.Item as GridDataItem;

            //Check the formatting condition
            if (dataBoundItem["sample_hour"].Text == "4hr YP")
            {
                if (float.Parse(dataBoundItem["ph"].Text) > 5.72 || float.Parse(dataBoundItem["ph"].Text) < 4.75)
                {
                    dataBoundItem["ph"].BackColor = Color.Yellow;
                    dataBoundItem["ph"].ForeColor = Color.Black;
                    dataBoundItem["ph"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["brix"].Text) > 22.36 || float.Parse(dataBoundItem["brix"].Text) < 17.35)
                {
                    dataBoundItem["brix"].BackColor = Color.Yellow;
                    dataBoundItem["brix"].ForeColor = Color.Black;
                    dataBoundItem["brix"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["temp"].Text) > 91 || float.Parse(dataBoundItem["temp"].Text) < 89)
                {
                    dataBoundItem["temp"].BackColor = Color.Yellow;
                    dataBoundItem["temp"].ForeColor = Color.Black;
                    dataBoundItem["temp"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["bud"].Text) > 41.76 || float.Parse(dataBoundItem["bud"].Text) < 3.121)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["cell_count"].Text) > 177.70 || float.Parse(dataBoundItem["cell_count"].Text) < 41.24)
                {
                    dataBoundItem["cell_count"].BackColor = Color.Yellow;
                    dataBoundItem["cell_count"].ForeColor = Color.Black;
                    dataBoundItem["cell_count"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["viability"].Text) > 69.183 || float.Parse(dataBoundItem["viability"].Text) < 5.65)
                {
                    dataBoundItem["viability"].BackColor = Color.Yellow;
                    dataBoundItem["viability"].ForeColor = Color.Black;
                    dataBoundItem["viability"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp4"].Text) > 10.892 || float.Parse(dataBoundItem["dp4"].Text) < 2.556)
                {
                    dataBoundItem["dp4"].BackColor = Color.Yellow;
                    dataBoundItem["dp4"].ForeColor = Color.Black;
                    dataBoundItem["dp4"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["dp3"].Text) > 6.052 || float.Parse(dataBoundItem["ph"].Text) < 1.412)
                {
                    dataBoundItem["bud"].BackColor = Color.Yellow;
                    dataBoundItem["bud"].ForeColor = Color.Black;
                    dataBoundItem["bud"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["maltose"].Text) > 8.285 || float.Parse(dataBoundItem["maltose"].Text) < .419)
                {
                    dataBoundItem["maltose"].BackColor = Color.Yellow;
                    dataBoundItem["maltose"].ForeColor = Color.Black;
                    dataBoundItem["maltose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glucose"].Text) > 6.695 || float.Parse(dataBoundItem["glucose"].Text) < -0.263)
                {
                    dataBoundItem["glucose"].BackColor = Color.Yellow;
                    dataBoundItem["glucose"].ForeColor = Color.Black;
                    dataBoundItem["glucose"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["lactic_acid"].Text) > .124 || float.Parse(dataBoundItem["lactic_acid"].Text) < .0101)
                {
                    dataBoundItem["lactic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["lactic_acid"].ForeColor = Color.Black;
                    dataBoundItem["lactic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["glycerol"].Text) > .574 || float.Parse(dataBoundItem["ph"].Text) < .332)
                {
                    dataBoundItem["glycerol"].BackColor = Color.Yellow;
                    dataBoundItem["glycerol"].ForeColor = Color.Black;
                    dataBoundItem["glycerol"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["acetic_acid"].Text) > 0.176|| float.Parse(dataBoundItem["acetic_acid"].Text) < -.0756)
                {
                    dataBoundItem["acetic_acid"].BackColor = Color.Yellow;
                    dataBoundItem["acetic_acid"].ForeColor = Color.Black;
                    dataBoundItem["acetic_acid"].Font.Bold = true;

                }
                if (float.Parse(dataBoundItem["ethanol"].Text) > 1.159 || float.Parse(dataBoundItem["ethanol"].Text) < .0053)
                {
                    dataBoundItem["ethanol"].BackColor = Color.Yellow;
                    dataBoundItem["ethanol"].ForeColor = Color.Black;
                    dataBoundItem["ethanol"].Font.Bold = true;

                }

            }

        }
    }

最佳答案

您所有的if语句都在做相同的事情,您可以将它们重构为一个方法。

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        //Get the instance of the right type
        GridDataItem dataBoundItem = e.Item as GridDataItem;

        //Check the formatting condition
        if (dataBoundItem["sample_hour"].Text == "4hr YP")
        {
            SetFormatting(dataBoundItem["ph"], 5.72, 4.75);
            SetFormatting(dataBoundItem["brix"], 22.36, 17.35);

            // etc...
        }

    }
}

private void SetFormatting(TableCell cell, float minValue, float maxValue)
{
    float value = float.Parse(cell.Text);

    if (value > minValue || value < maxValue)
    {
        cell.BackColor = Color.Yellow;
        cell.ForeColor = Color.Black;
        cell.Font.Bold = true;
    }
}

关于c# - 重构多个if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31973753/

10-16 09:04