[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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

0
December2000
Asked:
December2000
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
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.
0
 
AndyAinscowCommented:
Further to the comment by kaufmed re variables and scope.
You can always pass variables into methods:

int foo(int i)
{
  return i+42;
}

.....
int x = foo(7);
//x is 49


and return then as well
int foo(ref int i)
{
  i += 42;
}

int j = 7;
foo(j);
//j is now 49
0
 
December2000Author Commented:
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?);
0
 
käµfm³d 👽Commented:
what is an example of what you put here?
See Andy's last example. In the top bit of code, he has defined a method named foo, and this method takes one int for a parameter. This makes i, the single int parameter local to the method foo. No code outside of foo can see the variable i.

Next, in the few lines below the method definition, Andy shows how to call this new method (hint:  this would be similar to what you would put in your Main method). You call foo by using the method name followed by parentheses. If the method takes any parameters, you put those values and/or variables within the parentheses--in the order in which they are defined within the method definition. Since Andy only shows one parameter, there is only one parameter to pass. In this example, he is passing a "hard-coded" value to the method; however, you can actually pass variables as well (provided they are of the correct type). This is what he shows in the 2nd example. In passing a variable, the value that is actually stored within the variable is passed (i.e. foo receives the value 7). So if I instead did this:

int x = 22;
foo(x);

Open in new window


...then inside of foo on this particular invocation, foo would be working with the value 22--this is what i would have stored within it while foo is executing.
0
 
AndyAinscowCommented:
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.)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now