C# ref in constructor and change in method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ijrr
    New Member
    • Jan 2008
    • 3

    #1

    C# ref in constructor and change in method

    Hello all!

    I´m passing a reference using ref to a constructor in class A.
    A a = new A(ref myObj);
    If I change values of myObj in class A, they are reflected on the main class.

    But, If I call a method like:
    a.myMethod()
    And change values of myObj (a private variable inside class A, assigned from ref by the constructor), they are not reflected on the main class.

    Any work around? I dont want to use out or a return variable. It should work, because the reference was set in constructor.

    Thanks!
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Hi,
    it seems that you have not stored the myObj passed to class A when the constructor was called.

    you should have it somethwat like this

    [CODE=cpp]Class A
    {
    private object _myObj;
    public A (ref object myObj)
    {
    this._myObj = myObj;
    }
    public void myMethod()
    {
    //do what you want to with _myObj and it will work
    }
    }[/CODE]

    Comment

    • ijrr
      New Member
      • Jan 2008
      • 3

      #3
      Originally posted by Shashi Sadasivan
      Hi,
      it seems that you have not stored the myObj passed to class A when the constructor was called.

      you should have it somethwat like this

      [CODE=cpp]Class A
      {
      private object _myObj;
      public A (ref object myObj)
      {
      this._myObj = myObj;
      }
      public void myMethod()
      {
      //do what you want to with _myObj and it will work
      }
      }[/CODE]
      Shashi,
      I was creating a new instance of myObj in the method. If I create in the constructor, it will reflect OK in main class. Creating in method, it doesn´t reflect in main class, even using ref in the constructor.
      I think it should, because myObj is actually a pointer to the reference of the object (using ref). New instance should be visible when returning from method.

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Hi I havent got what you tried to mean.

        However i have created a console appplication to demonstrate what you may need

        If you post your code then we might be able to help out on what the issue could be
        Attached Files
        Last edited by Shashi Sadasivan; Jan 15 '08, 12:16 AM. Reason: adding attachment

        Comment

        • ijrr
          New Member
          • Jan 2008
          • 3

          #5
          Originally posted by Shashi Sadasivan
          Hi I havent got what you tried to mean.

          However i have created a console appplication to demonstrate what you may need

          If you post your code then we might be able to help out on what the issue could be
          Shashi,
          Thanks for the code. I changed it to reflect what I´m saying.
          The second print should be "Created by makeChanges()", because the object was passed as a reference in constructor. It should assign the new instance (it occurs if the new instance is created in the constructor).
          Seens C# lost the reference when it goes outside the method with ref, even if you assign the reference to a variable.
          Attached Files

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            Ok,
            Allright i see what you want.

            you want the class making changes to change the entire refrence to the object to a brand new one and then that should be refllected to the main class.

            In that case, I would say what it is doing is riight (by so called loosing the reference). Have you have a go with pointers in C ?

            Anyways this is something that can be explained very well on paper.

            What happens is that the main class creates an object of ObjectClass
            and passes the refrence to the class making changes. passing by refrence is passing the address of the object (ie where it is located physically)

            when you make changes to this object it makes the change to that specific address, and is hence reflected back to the main class (as even that is using the same address)

            now when you create a new object, a new memory location is allocated for it, and this memory address is assigned to it, so effectively the variable in the class making changes drops the address sent to it and takes the new one. And since this address does not match the one being sent by the main class, it is not reflected there as they are 2 seperate classes.

            The probable solution to this wouldbe to pass the object from the main class to this makechanges method as an out parameter. so this way the new object will be returned without having to create a return type.

            If not then you will have to create a new variable, and after you assign all values, deep copy it to the variable which is set to the object sent from the main class

            Comment

            Working...