?
Solved

Calling a method in C#

Posted on 2012-09-17
5
Medium Priority
?
388 Views
Last Modified: 2012-09-28
I am a Newbie to C# Experts....

I want to call cost from the typeWoodMethod to the deskCostMethod.... the part I cant get right is that I want to use the user response as part of the calculated total (or add it to the surcharges) How do I call part of a method?

using System;
public class DeskMethod
{
    public static void Main()
    {

        numberDrawersMethod();
        typeWoodMethod();
        DeskCostMethod();
        ReturnMethod();

        Console.ReadLine();
    }
    public static void numberDrawersMethod()
    // 
    {
        int drawers;
        string tDrawers;
        Console.WriteLine("How many drawers?  ");
        tDrawers = Console.ReadLine();
        drawers = Convert.ToInt32(tDrawers);
        Console.WriteLine("Number of drawers is {0} ", tDrawers);
        Console.ReadLine();

    }

    public static void typeWoodMethod()
    // get cost of wood by type
    {
        double cost;
        string wood;

        Console.WriteLine("What type of wood? Please enter 'm' for mahogany, 'o' for oak, or 'p' for pine. ");
        wood = Console.ReadLine();

        switch (wood)
        {
            case "o":
                cost = 150;
                break;
            case "p":
                cost = 140;
                break;
            default:
                cost = 180;
                break;
        }
        Console.WriteLine("Cost = " + cost.ToString());
        Console.ReadLine();

    }


    public static void DeskCostMethod()
    // array to select cost of wood type
    {
        double total;
        double wood;
        double surcharge;
        wood = 180;
        surcharge = 30.00;

        total = wood + surcharge;
        //return total;
        Console.WriteLine("Total Price of the drawer is {0}", total);
    }

    public static void ReturnMethod()
    {

        typeWoodMethod();

    }
}

Open in new window

0
Comment
Question by:December2000
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38407555
Perhaps if you return the cost from typeWoodMethod and capture that return value into a variable within Main. THEN, pass that variable to deskCostMethod (hint:  you need to make deskCostMethod accept a parameter), and you can use the variable (and implicitly the value it is storing) in place of the hard-coded "180" you have in line 60.
0
 

Author Comment

by:December2000
ID: 38407640
Thank you @kaufmed

I tried it and  got ...  Error1      No overload for method 'typeWoodMethod' takes 2 arguments
 
  public static void Main()
    {
        string wood;
        double cost;
        double mainCost;
            mainCost =  typeWoodMethod(wood,cost);
        numberDrawersMethod();
        typeWoodMethod();
        DeskCostMethod();
        ReturnMethod();

        

        Console.ReadLine();
    }

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 total points
ID: 38407688
OK. Let's start with your typeWoodMethod. What does it do? It prompts the user for the type of wood they would like, yes? And you get that choice from the user by prompting him and storing his answer to a string variable, yes? Do you really need that string anywhere else in your code, or is it just used to determine the base cost of the wood? Hopefully you agree that it's the latter. The string variable wood is a good candidate for a local variable. You already had this in line 31 or your original post. Let's keep that.

Now, what is the second piece of information that is discovered within the typeWoodMethod method? Would you agree that it is the cost of the wood? If so (and I hope you did agree), then let's examine the other code to see if we need to share this data. Is there another method which needs the cost? Yes, the DeskCostMethod  appears to need this value. This means that we in some way need to maintain the value we calculated based on the user's response. Well, functions can return things, so why not pass the calculated value back to the calling code (i.e. back to Main)? If we change your typeWoodMethod to return the cost instead:

public static double typeWoodMethod()    // LOOK AT WHAT'S DIFFERENT IN THIS LINE
// get cost of wood by type
{
    double cost;
    string wood;

    Console.WriteLine("What type of wood? Please enter 'm' for mahogany, 'o' for oak, or 'p' for pine. ");
    wood = Console.ReadLine();

    switch (wood)
    {
        case "o":
            cost = 150;
            break;
        case "p":
            cost = 140;
            break;
        default:
            cost = 180;
            break;
    }
    Console.WriteLine("Cost = " + cost.ToString());
    Console.ReadLine();

    return cost;            // ...AND HOW I ADDED THIS LINE
}

Open in new window


...then we can capture that value in Main. You are almost doing this with your most recent post. We simply need to remove that extraneous parameter (wood) since it is not needed (we made it a local variable):

public static void Main()
{
    double cost;
    double mainCost;
    
    mainCost =  typeWoodMethod(wood,cost);
    numberDrawersMethod();
    DeskCostMethod();
    ReturnMethod();



    Console.ReadLine();
}

Open in new window


So how does DeskCostMethod now use that value that we captured in Main? Well, you've already worked out how to pass variables and data to methods, can you think of how to modify the definition of DeskCostMethod to accept a parameter so that we can pass it the captured value held by mainCost? Once you do that, simply pass mainCost to the function (hint: pass it in line 8 of the snippet just above this paragraph).
0
 

Author Comment

by:December2000
ID: 38407774
How do you declare wood in main.... It is saying that it does not exist in the current context...tried  string wood and I get an error
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38407822
That's my fault. I forgot to remove it from the parameter list. Change line 6 of my last snippet to:

...
mainCost =  typeWoodMethod();
...

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question