Link to home
Start Free TrialLog in
Avatar of December2000
December2000

asked on

Methods in C#

Hi Experts,

I am method newbie .... How I use the main method to call the other four methods? Do I declare the variables in the main method or the "local" methods?

using System;
public class DeskMethod
{
    public static void Main()
    {
        string wood;
        Double wPrice;
        double drawers;
                
        Console.WriteLine("What type of wood would you like? Enter M for Mahagony, O for Oak, or P for Pine");
        wood = Console.ReadLine();
        wPrice = Convert.ToDouble(wood);
        Console.WriteLine ( "The number is {0}", wPrice);
        Console.ReadLine();
      
    }
    public static void InputMethod()
    // 
    {
       
        Console.ReadLine();
    }

    public static void typeWoodMethod()
    // 
    {
        
       
        
                


    }


    public static void DeskCostMethod()
    // array to select cost of wood type
    {
       

    }


   public static void ReturnMethod()
    // 
    {

    }


}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

How I use the main method to call the other four methods?
Add a call to the method you want to run.

e.g.

public static void Main()
{
    InputMethod();
    ReturnMethod();
}

Open in new window


Do I declare the variables in the main method or the "local" methods?
That depends on your need. You generally want to put variables in the most restrictive scope possible. This is so that you won't introduce subtle (a.k.a. hard-to-find) bugs into your code. The more places within your code that a variable can be seen, the easier it is to inadvertently overwrite a value that is needed by some other part of the code.
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of December2000
December2000

ASKER

ahh excellent... I am still a bit foggy on this... so I can I use "local variables " or pass the variables.   When I call the method  at the main method  what would you use between the brackets? InputMethod( what is an example of what you put here?);
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just a little remark.  My second example wasn't quite correct.
I defined it as
int foo(ref int i)

Open in new window

but didn't return a value, it should have been
void foo(ref int i)

Open in new window


ps.  You seem to be learning C#.  Asking questions is good.  I hope you also have one (or more) books to be working through.  The reason being is that you can only ask questions about things you know exist.  (Also there are some very good experts here with a vast knowledge.  There are also a few with rather less good knowledge.  Also even good experts can have a bad day.)