Asynchronous
Asynchronous

Reputation: 3977

How do I get the value from a variable which has it's values assigned in another class?

I need to have a class for Variables and Methods for the sake of manageability. The problem is I am not sure how I can access the value of the variable assigned in the method class.

Here is the variable class, it just holds variables:

namespace Classes
{
    class MyVariableClass
    {
        public string MyVariable { get; set; }
    }
}

Here is the method: I am calling an instance of the variable class in the method class so that I can assign the variable to the values of the method.

namespace Classes
{
    class MyMethods
    {
        MyVariableClass myVarClass = new MyVariableClass();

        public void DoSomeStuff(string myString1, string myString2)
        {
           myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
               ? myString2 : "String 2 has nothing!");
        }
    }
}

Finally, below is the Main Method: When I run this code, MyVariable returns null, I am assuming I am accessing the variable before it is assigned it's values?

How do I get the variable with the values assigned in the Method class?

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
           MyMethods myMethod = new MyMethods();
           MyVariableClass myVarClass = new MyVariableClass();

            string something = "something";
            string nothingHere = null;

            myMethod.DoSomeStuff(something, nothingHere);

            //I need to call MyVariable here
            //Or be able to access it's values assigned in the MyMethod Class.

            Console.Write(myVarClass.MyVariable);
            Console.ReadKey();
        }
    }
}

Upvotes: 0

Views: 83

Answers (3)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : You are working on two different objects.

  1. you are creating an object with an instance variable myVarClass in MyMethods class.
  2. you are creating one more new object with an instance variable myVarClass in Main() method.

Note : You should always remeber that in object oriented programming objects are independent and maintain their own copy so modifying one object parameters/properties doesnot effect the other object parameters or properties.

Solution : instead of creating two different object create only one object with instance variable myVarClass in Main() method and pass it to the myClass method.

so you should change your myClass method DoSomeStuff() as below to accept the instance variable of `MyVariableClass``

 public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
 {
   //do something
 }

from Main() method call the above method as below:

MyVariableClass myVarClass = new MyVariableClass();    
string something = "something";
string nothingHere = null;    
myMethod.DoSomeStuff(myVarClass, something, nothingHere);

Complete Code:

namespace Classes
{
    class MyMethods
    {              
        public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
        {
           myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
               ? myString2 : "String 2 has nothing!");
        }
    }
}

and Main Program should be :

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
           MyMethods myMethod = new MyMethods();
           MyVariableClass myVarClass = new MyVariableClass();

            string something = "something";
            string nothingHere = null;

            myMethod.DoSomeStuff(myVarClass, something, nothingHere);

            //I need to call MyVariable here
            //Or be able to access it's values assigned in the MyMethod Class.

            Console.Write(myVarClass.MyVariable);
            Console.ReadKey();
        }
    }
}

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Define you class as:

namespace Classes
{
    class MyMethods
    {
        public MyVariableClass MyVarClass {get; private set;}


        public void DoSomeStuff(string myString1, string myString2)
        {
           if(MyVarClass == null)
           {
              MyVarClass = new MyVariableClass();
           }

           MyVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
               ? myString2 : "String 2 has nothing!");
        }
    }
}

then use as:

namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
           MyMethods myMethod = new MyMethods();

            string something = "something";
            string nothingHere = null;

            myMethod.DoSomeStuff(something, nothingHere);

            //I need to call MyVariable here
            //Or be able to access it's values assigned in the MyMethod Class.

            Console.Write(myMethod.MyVarClass.MyVariable);
            Console.ReadKey();
        }
    }
}

Upvotes: 1

Luc Morin
Luc Morin

Reputation: 5380

The myVarClass member of MyMethods is private by default, so if you want to be able to call it from outside the class itself, then you need to make it public.

Once public, you'll able to do:

static void Main(string[] args)
{
   MyMethods myMethod = new MyMethods();
   MyVariableClass myVarClass = new MyVariableClass();

    string something = "something";
    string nothingHere = null;

    myMethod.DoSomeStuff(something, nothingHere);

    //I need to call MyVariable here
    //Or be able to access it's values assigned in the MyMethod Class.

    Console.Write(myMethod.myVarClass.MyVariable);
    Console.ReadKey();
}

Also, be careful with the fact that the myVarClass defined in main is a completely different object.

Cheers

Upvotes: 1

Related Questions