Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
      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)
            {
           
    }
    }
Posted
Updated 3-Mar-11 2:18am
v2

You can achieve it like this:

C#
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!
 
Share this answer
 
Comments
Lukann 3-Mar-11 8:18am    
Thank you. i'm sorry to bother you again but i forgot something.
Sergey Alexandrovich Kryukov 3-Mar-11 11:24am    
Looks reasonable, my 5.
And OP's variant violated D.R.Y. principle.
--SA
Hi Lukann,

here is optimized code.


C#
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)
   {
   }
}


Thanks,
Imdadhusen.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 3-Mar-11 8:24am    
Good answer! 5+
Lukann 3-Mar-11 8:31am    
Thank you for the response it is very much appreciated.
Sergey Alexandrovich Kryukov 3-Mar-11 11:25am    
Good, my 5.
And OP's variant violated D.R.Y. principle.
--SA
Try something like this (this is untested code, so you may need to tweak it a bit):

C#
//--------------------------------------------------------------------------------------------------
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;
} 


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.
 
Share this answer
 
v7
Comments
Manfred Rudolf Bihy 3-Mar-11 8:23am    
Best! 5+
Lukann 3-Mar-11 8:27am    
Thank you i will try this code later this is my first time using C# i find it kind of hard. the way it was before i got an error on lines


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))
Sergey Alexandrovich Kryukov 3-Mar-11 11:24am    
Good, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900