• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Passing by value

Hello

Can you tell me why the outcome of this program is 5?

public class PropertyHolder
{
    private int someProperty = 0;

    public int getSomeProperty()
    {
        return someProperty;
    }

    public void setSomeProperty(int propValue)
    {
        someProperty = propValue;
    }

}

public class PropertyTester
{
    public static int Main(string[] args)
    {
        PropertyHolder propHold = new PropertyHolder();

        propHold.setSomeProperty(5);

        Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());

        return 0;
    }


when in the Microsoft website i read this:

class PassingValByVal
{
    static void SquareIt(int x)
    // The parameter x is passed by value.
    // Changes to x will not affect the original value of myInt.
    {
        x *= x;
        Console.WriteLine("The value inside the method: {0}", x);
    }
    public static void Main()
    {
        int myInt = 5;
        Console.WriteLine("The value before calling the method: {0}",
           myInt);
        SquareIt(myInt);   // Passing myInt by value.
        Console.WriteLine("The value after calling the method: {0}",
           myInt);
    }
}


Thanks
0
Kokas79
Asked:
Kokas79
1 Solution
 
YurichCommented:
actually these two a complitely different cases...

to make your example of the same type as the from Microsoft, try this:

static void Main(string[] args)
{
   PropertyHolder propHold = new PropertyHolder();
   
   int myValue = 5;
   propHold.setSomeProperty( myValue );

   Console.WriteLine( "Your myValue before passing: " + myValue );
   Console.WriteLine("Property Value: {0}", propHold.getSomeProperty());
   Console.WriteLine( "Your myValue after passing: " + myValue );
}

public class PropertyHolder
{
private int someProperty = 0;

   public int getSomeProperty()
   {
      return someProperty;
   }

   public void setSomeProperty(int propValue)
   {
      someProperty = propValue * 2;
   }
}

now why you have 5 in your case...
You're passing a literal value (5) to a class and you are displaying a member of that class, which was of course assigned to 5. As soon as you declared your class:

PropertyHolder propHold = new PropertyHolder();

your propHold will hold the values you assign to its members untill it's set to null or collected later by the garbage collector.

Passing by value in MS example.
First, they don't use any classes - there're just two functions and one variable in each function.
They demonstrate that if you change value of passing variable in another function, original value will not get changed.
That's it - original value - in your example, you didn't have this original value at all - it was literal 5.
And later, you didn't try to change this passed value to anything, you just assigned your class member ot it.
And in the case of functions, as soon as the function is left out of focus ( } of the function) all variables are getting disposed... not in the case of a class.

Well that's basically what my example above is doing using your class approach:
1. you have two variables - one in your local Main function and another in your class
2. you pass this variable (as value) to your class
3. it's getting changed in the class
4. but it's not getting changed in your main function!
5. mistery is solved ;)

regards
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now