Kshitiz
Kshitiz

Reputation: 35

Access value in C# Console

I want to access the value in main for read and write. Is this possible? I used it as an object but I know object clears itself.

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

Upvotes: 1

Views: 112

Answers (3)

Kona-hioyta
Kona-hioyta

Reputation: 26

Let me explain first what a "static" member is.

The "static" keyword can make a ,class, method,variable static this does mean that every static value can be accessed without making a instance of the class. Now let me get to the point your code

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

first you declared two non static value's , this does mean that you can set or get those values when making a instance of the "Program" class.

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

Here you did make two new instances of the Program Class, nothing is wrong with that, but do you see what you are doing? In the instance of p you are setting the read variable, (which is a instance value) to the value "ASD", this does mean that only in the Instance of class "p" the value "read" does have the value "ASD". The value write in the Instance "p" is still empty, because you didn't set it.

Now you also create a P1 instance, here you set the "Instance value write" to "asdas", that does mean that only for the instance P1 the write member does have a value "asdas", however the read member does stay Empty, because it is a new Instance.

Lets see the full picture what you have done till now:

The instance p does now looks like this:

p.read = "ASD" p.write = ""

The instance P1 does now looks like this:

p1.read = "" p1.write = "asdas"

Now in the main method you'are trying to access those two values whitch are set in two different instances, what does not make sense. If you run the follow code you will probably understand what i am trying to say in the full explanation.

Please understand that the Solution for your problem is under this code. This is just a example of what i tried to explain above.

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

So finally after a bunch of text i will post a solution.

Solution :

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
    }

}

Upvotes: 0

Tinwor
Tinwor

Reputation: 7973

You cannot have an access to th p.Read and p.Write because they are visible only in the method Anymethod() So you have two possibilities: delcare Read and Write as static

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

And you can access to this fields like this:

Program.read
Program.write

2) you can pass P and P1 as ref parameters

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

In the main you can do something like this:

 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****
    }

Upvotes: 0

Muhammad Umar
Muhammad Umar

Reputation: 3781

I think you want to implement something like this.

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

Alternate Method

Try this

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

Upvotes: 1

Related Questions