Can someone help me with passing values from one class method to another in c#. I\'ve tried looking it up, but I get lost in pages of code and I cant find a simple example. Take
You could have the method return this value:
public static string getName()
{
Console.Clear();
Console.Write("Enter name: ");
return Console.ReadLine();
}
and then in your main program call the method and store the result in a local variable:
static void Main(string[] args)
{
string myName = Values.getName();
}
Notice that since getName
is a static method you do not need to create an instance of the Values
class in order to invoke it. You could directly call it on the type name.
If on the other hand the getName
method wasn't static:
public string getName()
{
Console.Clear();
Console.Write("Enter name: ");
return Console.ReadLine();
}
then you need the instance:
static void Main(string[] args)
{
Values values = new Values();
string myName = values.getName();
}