13 Replies - 568 Views - Last Post: 26 September 2012 - 07:29 PM Rate Topic: -----

#1 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Use methods only no code in event handler ?

Posted 23 September 2012 - 08:47 PM

Windows Application that will compute and display the Sales Tax and Total Sales amount
for a given price. The user should be allowed to enter the price and select his or her state's Sales
Tax Rate. When they click a Button, your program will calculate and display the amount of the
Sales Tax and the overall total, rounded to two decimal places formatted as currency in output
Labels. Must use methods only no code wirten in eventhandler

Just need to know if i have the right idea on the code or i am doing it all worng !!!


    public partial class taxrate : Form
    {
        public taxrate()
        {
            InitializeComponent();
        }
        // get int values from text box
        class GetCal
        {
            public int Caltaxrate(int txtSP, int txtTR)
            {
                return txtSP * txtTR;
            }

            public int GetTaxRate(int txtSPO, int txtSTA)
            {
                return txtSPO * txtSTA;
            }
            
        }
        // display values in text box
        private void btnCAl_Click(object sender, EventArgs e)
        {
            
           // how to get the methods to out put useing this btn?

        }

       

    }

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Use methods only no code in event handler ?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Use methods only no code in event handler ?

Posted 23 September 2012 - 09:07 PM

Technically you are doing it wrong because no matter what you do when you write:
void button_Click(object sender, EventArgs e)
{
    CalculateMethod();
}

void CalculateMethod()
{
    // Code do compute and update display here
}



Line 3 will be code in the event handler and the assignment stipulates "Must use methods only no code wirten in eventhandler".

You may want to verify with your instructor whether line 3 counts as code or not.

If even that single line counts as code, then what you'll have to do is subclass the Button class to create your own CalculateButton class. In the class, you'll need to override the protected onclick() method, and add code to initiate the computation there.

http://msdn.microsof...ol.onclick.aspx

Edit after: The forum software insist on making the onclick() method with lower case. It should be Pascal case as shown in MSDN.

This post has been edited by Skydiver: 23 September 2012 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Re: Use methods only no code in event handler ?

Posted 23 September 2012 - 09:09 PM

View PostSkydiver, on 23 September 2012 - 09:07 PM, said:

Technically you are doing it wrong because no matter what you do when you write:
void button_Click(object sender, EventArgs e)
{
    CalculateMethod();
}

void CalculateMethod()
{
    // Code do compute and update display here
}



Line 3 will be code in the event handler and the assignment stipulates "Must use methods only no code wirten in eventhandler".

You may want to verify with your instructor whether line 3 counts as code or not.

If even that single line counts as code, then what you'll have to do is subclass the Button class to create your own CalculateButton class. In the class, you'll need to override the protected onclick() method, and add code to initiate the computation there.

http://msdn.microsof...ol.onclick.aspx

ok i will ask see if line 3 will count as code thanks for the quick reply
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4428
  • View blog
  • Posts: 7,698
  • Joined: 08-June 10

Re: Use methods only no code in event handler ?

Posted 24 September 2012 - 05:55 AM

I'd guess that you're doing it right. The professor is probably trying to teach you (correctly) to not bog down your event handler methods with implementation.
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Use methods only no code in event handler ?

Posted 24 September 2012 - 07:21 AM

Yep. What if you have 300 event handlers that all do the same sequence of events? Put the sequence in a method, and call the method in each event handler.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Use methods only no code in event handler ?

Posted 24 September 2012 - 08:12 AM

Yes, it is a good practice to have event handlers be as thin as possible and have them call methods that actually have the implementation. But by the mere fact of calling the other method, you still have code in the event handler, albeit a very small amount of code. Is having a small amount of code like being a little pregnant?
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Use methods only no code in event handler ?

Posted 24 September 2012 - 10:15 AM

Outside my field really. You should ask on a pro-life forum.
Was This Post Helpful? 0
  • +
  • -

#8 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Re: Use methods only no code in event handler ?

Posted 25 September 2012 - 09:55 AM

Ok line 3 is allowed updated code but now haveing truble geting the try catch to work any sugestions would be helpful
{
    public partial class taxrate : Form
    {
        public taxrate()
        {
            InitializeComponent();
        }
        // get Decimal values from input text box
        decimal SalesPrice;
        decimal SalesTaxRate;
        decimal SalesTaxAmount;
        decimal Totalcost;

        //display the output

        private void btnCAl_Click(object sender, EventArgs e)
        {
            //get user data and validate
            //read in string data and convert to numeric format
            //Show message if bad data enterd
            //Extract the salesprice and sales tax rate
            GetUserDataInputAndValidate(ref  SalesPrice, ref  SalesTaxRate);
            CalCulate_Totalcost(ref  SalesTaxRate, ref SalesTaxAmount);

        }
        private decimal CalCulate_Totalcost(ref decimal SalesTaxAmount, ref decimal SalesPrice)
        {
            return SalesPrice + SalesTaxRate * SalesTaxAmount;
        }
        private void GetUserDataInputAndValidate(ref decimal SalesPrice, ref decimal SalesTaxRate)
         {
             // validate data
             try
             {

                 if (SalesPrice == 0)
                     return decimal ( String.Empty);
                 return  SalesPrice.ToString("C"); 
              if (SalesTaxRate == 0)
                     return decimal( String.Empty);
                 return SalesTaxRate.ToString("C");

             }
             catch (Exception ex)
             {
                 throw;
                 MessageBox.Show("Error in user input");

             }
         }




    }
}








the
Error 1 Invalid expression term 'decimal'
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Use methods only no code in event handler ?

Posted 25 September 2012 - 11:13 AM

Thank you for telling us what the error is, but you should also tell us what line the error is occurring on.

Just doing a visual inspection, there's a couple of issues.

On line 30, you are saying that that the method doesn't return anything by indicating a 'void' return type, but on lines 37, 38, 40, and 41 you are trying to return decimal or strings.

The decimal struct doesn't take strings in it's constructor: http://msdn.microsof...em.decimal.aspx

By the way, your event handler now has two lines of code. When does your teacher consider you having code in your event handler: 5, 10, 30 lines of code? Or are they not considered lines of code as long as they are always method calls? So if I have 40 lines all making method calls, that's just fine.
Was This Post Helpful? 0
  • +
  • -

#10 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Re: Use methods only no code in event handler ?

Posted 25 September 2012 - 03:39 PM

Hello

Thanks for reply the error is on line 54 and 57 is there not a way to convert the decmial to string so it pass into the try catch and then out to the output textbox's ?

Also the require two decmial place for output
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Use methods only no code in event handler ?

Posted 25 September 2012 - 04:08 PM

Look at the code you posted. What code is on lines 54 and 57?

http://msdn.microsof...l.tryparse.aspx
Was This Post Helpful? 0
  • +
  • -

#12 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Re: Use methods only no code in event handler ?

Posted 25 September 2012 - 04:59 PM

OOPS sorry i ment 37 and 40
Was This Post Helpful? 0
  • +
  • -

#13 tanky  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 02-August 12

Re: Use methods only no code in event handler ?

Posted 26 September 2012 - 05:33 PM

Hi
I got it to run with out errors but its not displaying the output if any one can see were the mistake is i be greatfull

         public taxrate()
        {
            InitializeComponent();
        }
        // get decimal values from input text box
        decimal SalesPrice;
        decimal SalesTaxRate;
        //Get Results
        decimal SalesTaxAmount;
        decimal Totalcost;

        private void btnCAl_Click_1(object sender, EventArgs e)
        {
            //get user data and validate
            //read in string data and convert to numeric format
            //Show message if bad data enterd
            //Extract the salesprice and sales tax rate
            GetUserDataInputAndValidate(ref SalesTaxAmount, ref SalesPrice);
            SalesTaxAmount = CalCulate_Totalcost(ref  SalesPrice, ref SalesTaxRate);
            Totalcost = DisplayResults(ref SalesPrice, ref SalesTaxAmount);
            DisplayResultsToGui(SalesPrice, SalesTaxAmount, Totalcost);
        }
    

        private decimal CalCulate_Totalcost(ref decimal SalesPrice, ref decimal SalesTaxRate)
        {
            return SalesPrice * SalesTaxRate;
        }

        private decimal DisplayResults(ref decimal SalesPrice, ref decimal SalesTaxAmount)
        {
            return SalesPrice + SalesTaxAmount;

        }
        private void DisplayResultsToGui(decimal SalesPrice, decimal SalesTaxAmount, decimal Totalcost)
        {
            txtSalePriceOut.Text = SalesPrice.ToString("c");
            txtTaxRateAmount.Text = SalesTaxAmount.ToString("c");
            txtTotalCost.Text = Totalcost.ToString("c");

        }

        private void GetUserDataInputAndValidate(ref decimal SalesPrice, ref decimal SalesTaxAmount)
        {
            try
            {


                if (SalesPrice == 0)
                    SalesPrice.ToString(txtSalePrice.Text);
                SalesPrice = decimal.Parse(txtSalePrice.Text);
                txtSalePrice.Text = SalesPrice.ToString("c");
                txtSalePriceOut.Text = SalesPrice.ToString("c");



                if (SalesTaxAmount == 0)
                    SalesTaxAmount.ToString(txtTaxRateAmount.Text);
                SalesTaxAmount = decimal.Parse(txtTaxRateAmount.Text);
                txtTaxRateAmount.Text = SalesPrice.ToString("c");

                if (Totalcost == 0)
                    Totalcost.ToString(txtTotalCost.Text);
                Totalcost = decimal.Parse(txtTotalCost.Text);
                txtTotalCost.Text = Totalcost.ToString("c");
            
            
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error in user input");
            }
        }

   
    }
}
    

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: Use methods only no code in event handler ?

Posted 26 September 2012 - 07:29 PM

I think you forgot to put in the code to read in and parse the SalesTaxRate, but that's just me taking a quick glance at the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1