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?
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()
//
{
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just a little remark. My second example wasn't quite correct.
I defined it as
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.)
I defined it as
int foo(ref int i)
but didn't return a value, it should have been
void foo(ref int i)
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.)
e.g.
Open in new window
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.