Methods

Part of a project

Page 1 of 1

3 Replies - 564 Views - Last Post: 04 December 2010 - 04:30 PM Rate Topic: -----

#1 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Methods

Posted 02 December 2010 - 10:22 PM

This program provides people with the APR % for the loan amount they enter, also changes depending on how many years you repay it over.

I do not have the best understanding of this language yet, I've only been using in for a couple of days. So I appreciate explanations as they help me grasp concepts.
        private void button1_Click(object sender, EventArgs e)
        {
            int loanAmount;
            int repayYears;
            loanAmount = Convert.ToInt32(tbox1.Text);
            repayYears = Convert.ToInt32(tbox2.Text);
            // Declares Variables
            
if (loanAmount == 1000)
    // ARP category 1
{
    if (repayYears == 1)
    {

        label3.Text = "6.5%";
    }
    else if (repayYears == 2)
    {
        label3.Text = "6%";
    }
    else if (repayYears == 3)
    {
        label3.Text = "5.8%";
    }
    else
    {
        label3.Text = "InvalidYear";
        // Validation for year box
    }
}
else if ((loanAmount <= 5000 ) && (loanAmount > 1000))
    // APR Category 2
{
    if (repayYears == 1)
    {
        label3.Text = "7.8%";
    }
    else if (repayYears == 2)
    {
        label3.Text = "7.5%";
    }
    else if (repayYears == 3)
    {
        label3.Text = "7%";
    }
    else
    {
        label3.Text = "InvalidYear";
        // Validation for year box
    }
}
else if ((loanAmount <= 25000) && (loanAmount > 5000))
    // APR Category 3 - APR With A Vengence
{
    if (repayYears == 1)
    {
        label3.Text = "12.7%";
    }
    else if (repayYears == 2)
    {
        label3.Text = "12%";
    }
    else if (repayYears == 3)
    {
        label3.Text = "11%";
    }
    else
    {
        label3.Text = "InvalidYear";
        // Validation for year box
    }
}
else
{
    label3.Text = "InvalidAmount";
    // Validation for  Amount box
}
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}


It works fine but I am required to "Use a Method to display the ARP for a particular loan size and duration". (Yes, it's a school project.)

However it's my understanding that methods are used to avoid repeating code... and I can't see how using methods could save time for this program. Then again, I probably have an incorrect understanding of how to use them.

The way I see it I have 3 categories with 3 subcategories in each.
}
private result a1((loanAmount > 1000) && ( loanAmount < 5000))
{
Would this work as a method? The idea being to define a loan amount catergory here so I can invoke it later with "a1" instead of having to do an if statement. Then doing the same for the Years so I could just do "if (a1 && a2) lable.text = whatever" etc to make the nest smaler.
}
private result a1();
private bool(int loanAmount)
{
if (loanAmount > 1000) && ( loanAmount < 5000)
{
     return true;
}
else
{
   return false;
}
)
Was also considering if boolean would make it easier... I've kind of lost my train of thought on this one.

Is This A Good Question/Topic? 0
  • +

Replies To: Methods

#2 Curtis Rutland  Icon User is offline

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


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Methods

Posted 02 December 2010 - 11:06 PM

Methods aren't only for reusing code, they're also for logically grouping tasks. One common philosophy for methods is that each should do one thing, and one thing only. One could calculate a rate, one could apply that rate to an amount, and one could call both in sequence.

So in this context, your button1_Click's job is to handle the click event. In that you would call GetRate, and pass it two ints: loanAmount and term. (Term is the timeframe...I work for a bank). That method doesn't have to know how to read text boxes or deal with strings. You'd get the ints in the click event and pass them to GetRate. Get rate will have your nested if statements (which you've already done good with). It could return a string, which you would apply to the rate label.

If I may suggest, rename your labels, buttons, and textboxes something useful. label3 doesn't mean anything, but rateLabel does.
Was This Post Helpful? 3
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Methods

Posted 03 December 2010 - 05:21 AM

A couple of ProTips™. :P

1. Change your control names to something more meaningful. A month down the line when you open this project you won't know what the hell button1 is, or what label3 is. Set it and forget it, I say.

2. You can create classes that act as holder objects. These are called POCO objects in C# (a take on POJO objects from Java). They make your code much more clean and best of all, it allows you to use Visual Studio's excellent intellisense help. Why not use it? :)

3. Instead of programming a business process in the EventHandler, it's much better to invoke a method. Why? Well right now the process is only fired when you click, but what happens when Mr. Bossy McBoss asks you, "Hey Sergio, I need to have a menu item up top that does the same thing." Will you copy and paste the same code in ANOTHER event handler? Or will you be a good programmer and invoke a method, LIKE A BOSS™? You know what to do!

4. I totally agree with Alias, your methods should only do 1 thing and one thing only. Don't have it concern itself with GUI and other things. Have it accept parameters do it's thing and spit something out.

It's so easy to fall into the habit of just doing things quick and forgetting about it, but building a great solution takes time and is an investment that will pay for itself if you let it.

I hope you follow our advice to become a better programmer. :)

"FALL DOWN 7 TIMES, STAND UP 8."

Edit: Your avatar looks delicious.

This post has been edited by Sergio Tapia: 03 December 2010 - 05:22 AM

Was This Post Helpful? 1
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Methods

Posted 04 December 2010 - 04:30 PM

Thanks for the advice!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1