我怎样才能使这段代码比这更好 [英] How can i make this code better than this

查看:83
本文介绍了我怎样才能使这段代码比这更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      private void GoButt_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                double VL = Convert.ToDouble(LO.Text);

                double VA = Convert.ToDouble(AO.Text);

                double VTS = Convert.ToDouble(VL * VA);

                double GypScrew0 = Convert.ToDouble();
              

                TSF.Text = VTS.ToString();
                TMS.Text = (VL * 12 / 16).ToString();
                TMT.Text = (VL * 2 / 10).ToString();
                TMSc.Text = (VL * 12 / 16 * 4).ToString();
                

             

                if (GSL.SelectedIndex.Equals(0) & CSL.SelectedIndex.Equals(0))

                    TGB.Text = (VTS / 32).ToString();
                   TGBS.Text = (VTS * 1.5 / 258).ToString();
                 
                else if (GSL.SelectedIndex.Equals(0) & CSL.SelectedIndex.Equals(1))
                    TGB.Text = (VTS / 32 * 2).ToString();

                else if (GSL.SelectedIndex.Equals(0) & CSL.SelectedIndex.Equals(2))
                    TGB.Text = (VTS / 32 * 3).ToString();

                else if (GSL.SelectedIndex.Equals(0) & CSL.SelectedIndex.Equals(3))
                    TGB.Text = (VTS / 32 * 4).ToString();


                else if (GSL.SelectedIndex.Equals(1) & CSL.SelectedIndex.Equals(0))
                    TGB.Text = (VTS / 36).ToString();

                else if (GSL.SelectedIndex.Equals(1) & CSL.SelectedIndex.Equals(1))
                    TGB.Text = (VTS / 36 * 2).ToString();

                else if (GSL.SelectedIndex.Equals(1) & CSL.SelectedIndex.Equals(2))
                    TGB.Text = (VTS / 36 * 3).ToString();

                else if (GSL.SelectedIndex.Equals(1) & CSL.SelectedIndex.Equals(3))
                    TGB.Text = (VTS / 36 * 4).ToString();


                else if (GSL.SelectedIndex.Equals(2) & CSL.SelectedIndex.Equals(0))
                    TGB.Text = (VTS / 40).ToString();

                else if (GSL.SelectedIndex.Equals(2) & CSL.SelectedIndex.Equals(1))
                    TGB.Text = (VTS / 40 * 2).ToString();

                else if (GSL.SelectedIndex.Equals(2) & CSL.SelectedIndex.Equals(2))
                    TGB.Text = (VTS / 40 * 3).ToString();

                else if (GSL.SelectedIndex.Equals(2) & CSL.SelectedIndex.Equals(3))
                    TGB.Text = (VTS / 40 * 4).ToString();



                else if (GSL.SelectedIndex.Equals(3)& CSL.SelectedIndex.Equals(0))
                    TGB.Text = (VTS / 44).ToString();

                else if (GSL.SelectedIndex.Equals(3) & CSL.SelectedIndex.Equals(1))
                    TGB.Text = (VTS / 44 * 2).ToString();

                else if (GSL.SelectedIndex.Equals(3) & CSL.SelectedIndex.Equals(2))
                    TGB.Text = (VTS / 44 * 3).ToString();

                else if (GSL.SelectedIndex.Equals(3) & CSL.SelectedIndex.Equals(3))
                    TGB.Text = (VTS / 44 * 4).ToString();
              


            {
            }
        }
catch(Exception)
            {
           
    }
    }

推荐答案

您可以这样实现:

You can achieve it like this:

private void GoButt_Click(object sender, RoutedEventArgs e)
{
    try
    {

        double VL = Convert.ToDouble(LO.Text);

        double VA = Convert.ToDouble(AO.Text);

        double VTS = Convert.ToDouble(VL * VA);

        double GypScrew0 = Convert.ToDouble();


        TSF.Text = VTS.ToString();
        TMS.Text = (VL * 12 / 16).ToString();
        TMT.Text = (VL * 2 / 10).ToString();
        TMSc.Text = (VL * 12 / 16 * 4).ToString();

        int idxGSL = GSL.SelectedIndex;
        int idxCSL = CSL.SelectedIndex;

        double tmp = (((idxCSL + 1) * VTS) / (32 + 4 * idxGSL));
        TGB.Text = String.Format("{0}",tmp);

    }
    catch(Exception)
    {
        //Do something meaningful here
    }
}



这就是所有真正需要的.

干杯!



That''s all that is really needed.

Cheers!


嗨 卢坎,

这是优化的代码.


Hi Lukann,

here is optimized code.


private void GoButt_Click(object sender, RoutedEventArgs e)
{
   try
   {
       double VL = Convert.ToDouble(LO.Text);
       double VA = Convert.ToDouble(AO.Text);
       double VTS = Convert.ToDouble(VL * VA);
       double GypScrew0 = Convert.ToDouble();

       TSF.Text = VTS.ToString();
       TMS.Text = (VL * 12 / 16).ToString();
       TMT.Text = (VL * 2 / 10).ToString();
       TMSc.Text = (VL * 12 / 16 * 4).ToString();

       /*Optimization start*/
       int GSL = 4 * GSL.SelectedIndex;
       int CSL = CSL.SelectedIndex + 1;
       TGB.Text = Convert.ToString(VTS / ( (32 + GSL) * CSL));            
       /*Optimization end*/ 
    }
catch(Exception)
   {
   }
}



谢谢,
Imdadhusen.



Thanks,
Imdadhusen.


尝试类似的方法(这是未经测试的代码,因此您可能需要对其进行一些调整):

Try something like this (this is untested code, so you may need to tweak it a bit):

//--------------------------------------------------------------------------------------------------
private void GoButt_Click(object sender, RoutedEventArgs e)
{
    double VL;
    double VA;
    double VTS;
    double GypScrew0;

    if (double.TryParse(LO.Text, out VL) &&
        double.TryParse(AO.Text, out VA) &&
        GSL.SelectedIndex >= 0 && 
        CSL.SelectedIndex >= 0)
    {
        VTS       = VL * VA;

        TSF.Text  = VTS.ToString();
        TMS.Text  = (VL * 12 / 16).ToString();
        TMT.Text  = (VL * 2 / 10).ToString();
        TMSc.Text = (VL * 12 / 16 * 4).ToString();
        
        TGB.Text  = CalcTGB(VTS, GSL.SelectedIndex, CSL.SelectedIndex).ToString();
    }
    else
    {
        throw new Exception("Something bad happened on the way to the Bijou Theater");
    }
}

//--------------------------------------------------------------------------------------------------
private double CalcTGB(double vts, int gslIndex, int cslIndex)
{
    double result = 0d;
#if DEBUG
    int divider    = 32 + (4 * cslIndex);
    int multiplier = cslIndex + 1;
    result         = vts / (double)((divider * multiplier));
#else
    result         = vts / (double)((32 + (4 * cslIndex)) * (cslIndex + 1));
#endif
    return result;
} 



编辑===============

我的方法允许您在调试器中逐步检查它,以确保乘法器和除法器与您期望的一样.

最后说明-将用户指定的数据转换为数值时,应真正使用double.TryParse(而不是Convert.ToDouble).这将消除对try/catch块的需要.

编辑===============

我将示例修改为使用TryParse而不是Convert.ToDouble.



EDIT ===============

My method allows you to step through it in the debugger to make sure the multiplier and divider are what you expect them to be.

Final note - you shoud realy use double.TryParse (instead of Convert.ToDouble) when you''re converting user-specified data to numeric values. This will eliminate the need for the try/catch block.

EDIT ===============

I modified my example to use TryParse instead of Convert.ToDouble.


这篇关于我怎样才能使这段代码比这更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆