# Calling Parts of Methods

• (2 Pages)
• 1
• 2

## 21 Replies - 1260 Views - Last Post: 23 August 2011 - 06:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=244507&amp;s=89c8b066f3c231039201faafb4781eb8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 kimmi_baby

Reputation: 2
• Posts: 87
• Joined: 18-April 11

## Re: Calling Parts of Methods

Posted 23 August 2011 - 05:08 AM

karabasf, on 23 August 2011 - 04:38 AM, said:

kimmi_baby, on 23 August 2011 - 12:56 PM, said:

karabasf - Thank you for your help I'll think about what you said and give it a go!

Glad I could help, just try and give it a go

kimmi_baby, on 23 August 2011 - 01:31 PM, said:

This is probably a silly question.. but should I be putting the if statement in the method I would like to use part of (GetDimensions())? Or the method I'd like to add it to (DetermineSquareMetres())?

One thing to say: STOP!

What exactly are you trying to achieve with the conditional (if..else) statements? As I could see from your previous post, you would like to use the length and the width for your calculation. But why would you want to trigger it via an if statement? Think about that (and off course if you need such a statement at all) and you probably find out an answer by yourself.

Personally, I always see a method as a small operation, which is part of a bigger operation. Using your exercise as an example, you can define some steps, which a user needs to take before the whole operation is done.

1) Show instructions
2) Get dimensions
3) Calculate area

Now, step 2 can be divided up in several more steps (which all can be put either in one method or separate methods for re-usage)

2.1) Get length
2.2) Get width

Off course, this can be extended with data validation and so on.

Well this is what I have worked out so far:
DisplayInstructions
GetDimensions for length
GetDimensions for width
GetPrice of carpet
DetermineSquareMetres(length, width)
DetermineCost(squareMetres, price)
DisplayResults(squareMetres, cost)

I would really like to do them in this order.. Now I know I will need to use results from multiple methods in each method.

DetermineSquareMetres needs GetDimensions()
DetermineCost needs DetermineSquareMetres and GetPrice
DisplayResults will need DetermineSquareMetres and DetermineCost

Now as for the if statements, I somehow need a way to pull those certain values or results from each method. I'm looking at the different if statements to see if any of them do what I'm needing.. Am I heading in the right direction?

### #17 rgfirefly24

• D.I.C Lover

Reputation: 340
• Posts: 1,713
• Joined: 07-April 08

## Re: Calling Parts of Methods

Posted 23 August 2011 - 05:18 AM

The only way if statements are going to help with the GetDimensions() is if you passed in an argument of what you were wanting the user to enter, but that is a little silly.

You want to get the length and width from the user by calling GetDimensions. You have two choices for extracting those units back out of GetDimensions. Having a return type, or using global variables. Now because you are asking for two different values within one method you will need to return them together in an object of some sort. I would suggest a Dictionary so that way you can know which value is which.

As far as global variables, that would be an easier solution as you declare the variables outside of any method, and then set thier values within GetDimensions.

### #18 Bloosdhed

Reputation: 17
• Posts: 22
• Joined: 18-August 11

## Re: Calling Parts of Methods

Posted 23 August 2011 - 05:28 AM

This would be the time you would want to make a class for your "Carpet Order"

for example:

```        public class CarpetOrder
{
//TODO: Fields/Properties & Validation
public string Name { get; set; }
public double Length { get; set; }
public double Width { get; set; }
public double SquareMetrePrice { get; set; }

public CarpetOrder(string name)
{
Name = name;
Length = 0;
Width = 0;
SquareMetrePrice = 14.95;
}

public double CalculateSquareMeters()
{
return Length*Width;
}

public double CalculateCost()
{
return SquareMetrePrice*CalculateSquareMeters();
}

public string PrintOverview()
{
var overview = new StringBuilder();
overview.AppendFormat("Order: {0}", Name);
overview.AppendFormat("\nDimensions: W:{0}xL:{1} ({2}m²)", Width, Length, CalculateSquareMeters());
overview.AppendFormat("\nPrice per m² is {0}", SquareMetrePrice.ToString("C"));
overview.AppendFormat("\nTotal: {0}", CalculateCost().ToString("C"));

return overview.ToString();
}
```

This will make the code in your main section alot easier to follow, and if you need to modify something related to the carpet and its calculation order its all in a seperate class (file).

### #19 karabasf

• D.I.C Regular

Reputation: 202
• Posts: 417
• Joined: 29-August 10

## Re: Calling Parts of Methods

Posted 23 August 2011 - 06:16 AM

kimmi_baby, on 23 August 2011 - 02:08 PM, said:

Well this is what I have worked out so far:
DisplayInstructions
GetDimensions for length
GetDimensions for width
GetPrice of carpet
DetermineSquareMetres(length, width)
DetermineCost(squareMetres, price)
DisplayResults(squareMetres, cost)

I would really like to do them in this order.. Now I know I will need to use results from multiple methods in each method.

DetermineSquareMetres needs GetDimensions()
DetermineCost needs DetermineSquareMetres and GetPrice
DisplayResults will need DetermineSquareMetres and DetermineCost

Now as for the if statements, I somehow need a way to pull those certain values or results from each method. I'm looking at the different if statements to see if any of them do what I'm needing.. Am I heading in the right direction?

In pseudocode, you'd probably want something like this:
```DisplayInstructions

Prompt user to enter geometric parameter length [method]
return length
Assign the value of length to a parameter [method]
Prompt user to enter geometric parameter width
return width
Assign the value of width to a parameter
Prompt user to enter the price [method]
return price
Assign the value of price to a parameter

Calculate area (length, width) [method]
return area
Assign the value of area to a parameter
Calculate price (area) [method]
Assign the value of price to a parameter

Display (area, price)

```

Probably not 100% correct pseudocode, but this is basically what you want.

What Bloosdhed proposed is also a good solution. A class is basically an object holding some properties (like length, width, name and price) and its methods are used to perform operations with those properties.

As to say, you're problem might look simple on the first sight, but as you can see, there are many proposals about how to solve them. It's all about how you tackle it on a good manner.

### #20 tlhIn`toq

Reputation: 5858
• Posts: 12,755
• Joined: 02-June 10

## Re: Calling Parts of Methods

Posted 23 August 2011 - 06:56 AM

After 2 full pages of help on trying to explain the reasons you use methods in general, how to analyse your needs/process and turn that into pseudocode, how to turn an idea into a program and basically just a lot of help trying to get the basic concepts... Basically a lot of help with how to architect a program, not how to fix a specific code issue.

I have to urge you to stop trying to build this program from scratch. You need to worry about learning right now; not designing.

You don't read about the break and accelerator peddles then immediately jump in the freeway for an interstate drive.

### #21 kimmi_baby

Reputation: 2
• Posts: 87
• Joined: 18-April 11

## Re: Calling Parts of Methods

Posted 23 August 2011 - 06:16 PM

I'd like to thank JackOfAllTrades, rgfirefly24 and karabasf. With your help I was able to complete the program I'm quite happy with the end result. I'm very grateful for all your help

In saying that, I have never before had a problem with this forum and I normally come here for help on a variety of things but it has come to my attention that some people just aren't willing to help even though they feel the need to post to tell me not to continue with what I'm doing and in result has made me feel very uncomfortable being here. I will no longer use this forum as I feel that I can't come here for help without being judged or being deterred from from I want to do.

But thank you to the people that did help me, I appreciate it so much.

### #22 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4641
• Posts: 8,192
• Joined: 08-June 10

## Re: Calling Parts of Methods

Posted 23 August 2011 - 06:53 PM

Well, I suggest to you this: if you think light criticism and good advice are bad things, you need to grow a thicker skin. Many of us here are professionals; we've seen enough to know what we're talking about. When we suggest you try learning certain things before attempting what you're currently working on, it's not coming from a mean place. It's coming from an honest place. I mean, look at the rep numbers. The person you're upset with has almost 1800 points, each thanks for helping people. If you can't accept that he knows what he's talking about and take his suggestions for what they're worth, you need to reevaluate your attitude. Don't get upset with criticism. Take it to heart and use it to become a better programmer.

Or not, the choice is up to you. Regardless, as you've stated that your use for this topic is at an end, I'll close it.