# Calling Parts of Methods

• (2 Pages)
• 1
• 2

## 21 Replies - 1248 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=57719f38571bcb91cc0be7f194110f9c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kimmi_baby

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

# Calling Parts of Methods

Posted 23 August 2011 - 01:23 AM

Hi,
I have a couple of questions about calling values from methods. I'm not sure if I'm even wording this right but I hope someone will understand what I mean. The code is below.

```
using System;

namespace CarpetCalculatorExample {
class CarpetCostCalculatorUsingMethods {
static void Main() {

double roomWidth;
double roomLength;
double pricePerSqMetre;
double noOfSqMetres;
double carpetCost;

DisplayInstructions();
GetDimensions();
GetPrice();
DetermineSquareMetres(0, 0);

Console.Write("\nPress any key to terminate program");
}

public static void DisplayInstructions() {
Console.WriteLine("This program will determine how much carpet to purchase.");
Console.WriteLine();
Console.WriteLine("You will be asked to enter the size of the room,"
+  "length and breadth in metres. ");
Console.WriteLine("and the price of the carpet, as price per square metres.");
Console.WriteLine();
}

public static void GetDimensions() {
Console.Write("Enter the length in metres: ");
double length = double.Parse(Console.ReadLine());
Console.Write("Enter the width in metres: ");
double width = double.Parse(Console.ReadLine());

}

public static void GetPrice() {
Console.Write("Enter the price per square metre: \$");
double pricePerSqMetres = double.Parse(Console.ReadLine());

}

public static void DetermineSquareMetres(double length, double width) {

double noOfSqMetres;

noOfSqMetres = length * width;

Console.WriteLine("Square Metres needed: " + noOfSqMetres);

}

}
}

```

I am struggling with DetermineSqMetres as what I'm really wanting to do is multiply the length and width which the user inputs in GetDimensions. I'm not quite sure how to call that result from GetDimensions.

I've also noticed, when I call DetermineSqMetres in Main, I've had to put a value in the parameters otherwise I keep getting "No overload for method 'DetermineSqMetres takes 0 arguments". So at this moment, I've put 0 and 0 in there because it only seems to name a number value. I realise this doesn't work, but I'm not quite sure how else to fix it.

Any help would be appreciated!

Thanks

Is This A Good Question/Topic? 0

## Replies To: Calling Parts of Methods

### #2 karabasf

• D.I.C Regular

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 01:33 AM

It is possible to call a method in a method (it almost sounds like inception...)

As a result this will give you:
```public static void GetDimensions()
{
Console.Write("Enter the length in metres: ");
double length = double.Parse(Console.ReadLine());
Console.Write("Enter the width in metres: ");
double width = double.Parse(Console.ReadLine());

DetermineSquareMeters(length, width);
}

public static void DetermineSquareMetres(double length, double width)
{
double noOfSqMetres;
noOfSqMetres = length * width;
Console.WriteLine("Square Metres needed: " + noOfSqMetres);
}

```

Using this allows you to call the method GetDimensions(), which also calculates the area.

However, I am not sure if I would recommend this solution though, as it is recommended that each method should do one thing. Per example, suppose I want to use the data of GetDimensions for something else. Instead of actual returning the dimensions, it gives me the area instead.

At least, I hope that this helps you out

### #3 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 01:54 AM

Hi there

Thank you for your reply but I definitely agree with you. I'm aiming to have each method do one thing.

I also have 2 more methods to go. One that will calculate the cost and one that will display results.

I thought what I was wanting to do would be something quite simple and just that I didn't know how to do it.. I really hope it's possible!

Do I have to actually call the whole method? or can I call parts of it? the stored result in length and width?

### #4 Ace26

Reputation: 40
• Posts: 183
• Joined: 10-August 08

## Re: Calling Parts of Methods

Posted 23 August 2011 - 02:05 AM

This resource will help you a lot.

### #5 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 02:40 AM

Thanks for the link Any suggestions as to where I should be looking? I looked in the methods section and it doesn't appear to be in there.

### #6 tlhIn`toq

• Please show what you have already tried when asking a question.

Reputation: 5839
• Posts: 12,693
• Joined: 02-June 10

## Re: Calling Parts of Methods

Posted 23 August 2011 - 03:00 AM

Calling methods, determining the right signature (return type and parameters), etc. are basic concepts covered in the first couple chapters of any self-teaching book. That you are having trouble with this indicates, to me anyway, that you are trying to jump in and build a program before actually working through a book from cover to cover just doing the lessons. This is a bad way to go about learning to code.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

We don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).

Resources, references and suggestions for new programmers. - Updated Aug 2011
Spoiler

### #7 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 03:08 AM

Thank you for your reply. I completely understand this but I am actually working from a book. It gave an example to continue on from. I have done quite a few exercises before this one and I've been doing fine until now. I was just hoping someone could point me in the right direction..

I didn't think I was actually having trouble with return types.. I've only now come across a problem as I've just started to create methods. I had no problem doing everything in main. I'm just now trying to put it all into methods. Which from what I've read on numerous websites and books that methods are a good way to go for easy to read code.

### #8 karabasf

• D.I.C Regular

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 03:40 AM

Methods are a good use of programming as they reduce the amount of maintenance. Believe me, you do NOT want to correct copy paste codes, seeing dozen same lines. When it works it's ok, but when you need to correct it, it's a nightmare of the first class

Looking at your code and requirements, I'd say that I would create a method which returns an user input value, while printing out what kind of dimension parameter the user should enter. In this way you can keep it general.

As I am one who let you think first, a spoiler is good enough for now
Spoiler

You probably need to rewrite and might need to rethink the architecture of your program when you implement this.

This post has been edited by karabasf: 23 August 2011 - 03:42 AM

### #9 tlhIn`toq

• Please show what you have already tried when asking a question.

Reputation: 5839
• Posts: 12,693
• Joined: 02-June 10

## Re: Calling Parts of Methods

Posted 23 August 2011 - 03:48 AM

Think about this... how would you code a program if you didn't have methods?

It would *have* to be one long main() - which is itself a method.
Meaning your entire program would have to be a linear process from start to finish, with no branches and no real ability to back up, or do different things based on user input.

Do you know any program that works that way? Your web browser... your email client... a calculator...

Every time you click a button, a method is called to handle that one event.
A method is called to paint a control.
A method is called when a form resizes, so you can reposition and resize the controls to use the available space.
A method is called when you pick a menu item.

### #10 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 03:56 AM

I know that methods are important and that they are used for everything.. Which is why I'm trying to learn how to create them with this exercise.

I never said that I thought methods aren't important. I just said that I have completed this exercise before but as one method (Main). I am now trying to redo the exercise by creating multiple methods to simplify the code. Except I'm finding it a bit more complicated as I still don't quite understand how to call the results from other methods.

I understand that I can call the whole method within another method but that's not what I'm wanting to do. I'm wanting to just a certain part of the method if it's possible.

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

• Saucy!

Reputation: 6156
• Posts: 23,789
• Joined: 23-August 08

## Re: Calling Parts of Methods

Posted 23 August 2011 - 04:12 AM

If you only want to call part of one method, then you will need to extract that part into another entirely different method. If you only want to call a part of a method based on some conditions, well then that what if() {} else {} blocks are for.

### #12 rgfirefly24

• D.I.C Lover

Reputation: 336
• Posts: 1,689
• Joined: 07-April 08

## Re: Calling Parts of Methods

Posted 23 August 2011 - 04:19 AM

The only way you can trigger just a certain part of a method is to put conditional statements (if statements) within the method then have some kind of parameter that will tell that statement which section to execute.

Also, Your variables within GetDimensions() and GetPrice() are scoped to only the method they are created in, so they will not be seen or usable once you finish calling GetDimensions(). What i would suggest is to use global variables or return a list<int> to get those values out.

### #13 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 04:22 AM

Thank you JackOfAllTrades! Just to clarify, so if GetDimensions() asks the user to input length and width, and I would like to use the length and width for a calculation in DetermineSquareMetres(), I could use an if() else {}?

Thanks again

rgfirefly24 thank you! I'll give that a go

This post has been edited by kimmi_baby: 23 August 2011 - 04:31 AM

### #14 kimmi_baby

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 04:31 AM

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())?

### #15 karabasf

• D.I.C Regular

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

## Re: Calling Parts of Methods

Posted 23 August 2011 - 04:38 AM

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.

This post has been edited by karabasf: 23 August 2011 - 04:43 AM