C#Console中的访问值

时间:2013-11-16 17:33:17

标签: c# console

我想访问main中的值读取和写入。 这可能吗?我用它作为对象,但我知道对象会自行清除。

    class Program
    {  
        string read= String.Empty;
        string write= String.Empty;
        static void Main(string[] args)
        {

            Anymethod();
            Console.WriteLine(**read +write** ); **// **error I want to access the values of read and write from anymethod****
        }


         public static string Anymethod()
         {
                Program P = new Program();
                Program P1 = new Program();
                p.read = "ASD"
                p1.write="asdas";
         }
    }

3 个答案:

答案 0 :(得分:1)

我想你想要实现这样的东西。

public class Program
{

    static string read = string.Empty;
    static string write = string.Empty;

    static void Main(string[] args)
    {
        read = asad;
        write = ASAD;
        Console.WriteLine(read + write); 
    }
}

替代方法

试试这个

public class Program
{

    public string read = string.Empty;
    public string write = string.Empty;

    static void Main(string[] args)
    {
        Console.WriteLine(AnyMethod().read + AnyMethod().write); 
    }

    public static Program AnyMethod()
    {
        Program p = new Program();
        p.read = "Asad";
        p.write = "ASAD";
        return p;
    }
}

答案 1 :(得分:0)

您无权访问p.Readp.Write,因为它们仅在方法Anymethod()中可见 因此,您有两种可能性:delcare ReadWrite为静态

static string read=string.Empty;
static sring write=string.Empty;

您可以访问以下字段:

Program.read
Program.write

2)你可以传递P和P1作为参数

public static string Anymethod(ref Program P, ref Program P1)
     {
            P = new Program();
            P1 = new Program();
            p.read = "ASD"
            p1.write="asdas";
     }

在主要内容你可以这样做:

 static void Main(string[] args)
    {
        Program P,P1;
        Anymethod(ref P,ref P1);
        console.writeline(P.Read,P1.Write ); **// **error I want to access the values of read and write from anymethod****
    }

答案 2 :(得分:0)

首先让我解释一下“静态”成员是什么。

“static”关键字可以使a,class,method,variable static这意味着可以在不创建类的实例的情况下访问每个静态值。现在让我谈谈您的代码

    string read=string.empty;
    string write=string.empty;

首先你声明了两个非静态值,这意味着你可以在制作“程序”类的实例时设置或获取这些值。

 public static void Anymethod()
     {
            Program P = new Program();
            Program P1 = new Program();
            p.read = "ASD"
            p1.write="asdas";
     }

在这里你确实制作了两个新的程序类实例,没有任何问题,但你看到你在做什么吗?在p的实例中,您将读取变量(即实例值)设置为值“ASD”,这意味着仅在类“p”的实例中将值“读取” “确实有”ASD“的价值。实例“p”中写入的值仍为空,因为您没有设置它。

现在你也创建一个P1实例,在这里你将“Instance value write”设置为“asdas”,这意味着只有实例P1的写成员确实有一个值“asdas”,但是读成员确实保持空,因为它是一个新的实例。

让我们看看你到目前为止所做的一切:

实例p现在看起来像这样:

p.read =“ASD” p.write =“”

实例P1现在看起来像这样:

p1.read =“” p1.write =“asdas”

现在在主方法中,您试图访问在两个不同实例中设置的这两个值,这是没有意义的。如果您运行以下代码,您可能会理解我在完整解释中要说的内容。

请理解您的问题的解决方案是在此代码下。这只是我上面尝试解释的一个例子。

class Program
{

    private static Program P;
    private static Program P1;

    string read = string.Empty;
    string write = string.Empty;

    static void Main(string[] args)
    {

        Anymethod();

        Console.WriteLine("==================P instance value=================");

        Console.WriteLine("Value read of instance 'P'  P.read ='{0}'", P.read);
        Console.WriteLine("Value write of Instance 'P' P.write='{0}'", P.write);

        //Same as the above code, only not using a String format
       // Console.WriteLine("Value read of instance 'P'  P.read ='" + P.read + "'");
       // Console.WriteLine("Value write of Instance 'P' P.write='" + P.write + "'");

        Console.WriteLine("==================P1 instance value===================");
        Console.WriteLine("Value read of instance 'P1'  P1.read ='{0}'", P1.read);
        Console.WriteLine("Value write of Instance 'P1' P1.write='{0}'", P1.write);

        //Same as the above code, only not using a String format
        // Console.WriteLine("Value read of instance 'P1'  P.read ='" + P1.read + "'");
        // Console.WriteLine("Value write of Instance 'P1' P.write='" + P1.write + "'");

        Console.WriteLine("==============Together=================");
        Console.WriteLine(P.read + P1.write);



        Console.ReadLine();

    }


    public static void Anymethod()
    {
        P = new Program();
        P1 = new Program();
        P.read = "Hello ";
        P1.write = " World";
    }
}

所以最后在一堆文字后我会发布一个解决方案。

解决方案:

class Program
{
    public static string read = String.Empty; // Static, what does mean that every one can access it without making a instance
    public static string write = String.Empty; // Static, what does mean that every one can access it without making a instance

    static void Main(string[] args)
    {

        Anymethod();
        Console.WriteLine(read + write);
        Console.ReadLine();
    }


    public static void Anymethod()
    {
        Program.read = "Hello "; // Access the public static variable read in the ProgramClass
        Program.write = "World"; // Access the public static variable write in the Program Class
    }

}