0

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 a look at the following:

//Main Program Program.cs
namespace simplecode
{

    class Program
    {
        static void Main(string[] args)
        {
            Values Values = new Values();
            Values.getName();
        }
    }
}


//Separate class file or object Values.cs 


namespace simplecode
{
    public class Values
    {
        public static void getName()
        {
            Console.Clear();
            Console.Write("Enter name: ");
            string myName = Console.ReadLine();
        }
    }
}

Now, if I execute this the main program runs getName and I can store the myName variable. How do I grab the value of myName from the main program, or another separate class method? Im trying to understand this conceptually and I need the proper syntax. If someone could explain whats needed or provide a study link id appreciate it.

Thanks Mike

3 Answers 3

2

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

and the programmer actually doesn't need to create a new Values() as the method is static
@canpoyrazoğlu, good point. I will update my answer to include this remark.
thanks guys, I get the error of the void return type. If I change the type to string and return getName I get the error non delegate type...
that worked thank you very much. In previous projects i was trying to return the method name rather than the "return Console.ReadLine()". I think the logical way to say this is that you need to return the LOCAL value correct? and if its an input value then "return Console.ReadLine()". I hope I got that right.
1

First you need to actually store the variable somewhere. Right now it's a local variable in the method, so it will go away right after you have assigned the value to it.

Make it a property in the class, and make the method an instance method instead of a static method:

namespace simplecode
{
    public class Values
    {

        public string MyName { get; set; }

        public void getName()
        {
            Console.Clear();
            Console.Write("Enter name: ");
            MyName = Console.ReadLine();
        }
    }
}

Now you can call the method and pick up the value afterwards:

namespace simplecode
{

    class Program
    {
        static void Main(string[] args)
        {
            Values myValues = new Values();
            myValues.getName();
            Console.WriteLine(myValues.MyName);
        }
    }
}

5 Comments

This is also VERY helpful thank you. My current project is a simple login console app and I will most likely have to use your method because I dont want the values to go away.
In essence the line: public string MyName { get; set; } , is just an "empty container" right?
@MikeB: It's shorthand for a parameter with a backing variable, i.e. private string _myName; public string MyName { get { return _myName; } set { _myName = value; } }, but the private backing variable has a secret compiler generated name.
ok that makes sense that its a shorthand or shortcut, but the purpose for the compiler generated name is just to be an empty container that can be recalled at anytime instead of being destroyed if compiled in the method. Correct?
@MikeB: A property is just the object oriented way to handle a member variable. It's a member of the class, so each class instance gets one, and it lives as long as that instance lives.
0

Typically you would return the value from the function. You will want to think how your code calls different modules.

You may find if the flow is complicated you may want to look in to inversion of control, although off topic and more advanced it is related to how code is chained together and that I feel is your underlying question/issue.

If you had more than one value to return you could use a struct/class or mark parameters as ref allowing you to modify from within the function (variables passed by reference, as opposed to passing them by value which is done by default).

1 Comment

I think this is what was confusing me. I noticed alot of instruction on the OUT and REF statement, but now im thinking I just dont have a clear understanding of the return. I was trying to return the actual method name, as compared to return Console.ReadLine();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.