简体   繁体   English

在方法之间传递变量值...?

[英]Passing variable values between methods…?

Can someone help me with passing values from one class method to another in c#. 有人可以帮我在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. 现在,如果执行此操作,则主程序将运行getName,并且可以存储myName变量。 How do I grab the value of myName from the main program, or another separate class method? 如何从主程序或其他单独的类方法中获取myName的值? 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. 如果有人可以解释需要什么或提供研究链接ID,请多加赞赏。

Thanks Mike 谢谢迈克

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. 请注意,由于getName是静态方法,因此无需创建Values类的实例即可调用它。 You could directly call it on the type name. 您可以直接在类型名称上调用它。

If on the other hand the getName method wasn't static: 另一方面,如果getName方法不是静态的:

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();
}

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);
        }
    }
}

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). 如果要返回的值不止一个,则可以使用结构/类或将参数标记为ref,以允许您在函数内进行修改(变量通过引用传递,而不是通过值默认传递变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM