C# Object Reference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MiddleTommy
    New Member
    • May 2007
    • 5

    C# Object Reference?

    I know this is possible in C++ with pointers but dont know how to do it in C#.

    Simplified Sample Code and Comments

    //create object A
    object A = new object();

    //create object B
    object B = A;

    //create object C
    object C = new object();

    //I need to change Object A to point to the contents of Object C only using Object B
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why do you need two things pointing to the same object? I've not been able to come up with a use for it.

    Comment

    • mwalts
      New Member
      • May 2007
      • 38

      #3
      Originally posted by MiddleTommy
      I know this is possible in C++ with pointers but dont know how to do it in C#.

      Simplified Sample Code and Comments

      //create object A
      object A = new object();

      //create object B
      object B = A;

      //create object C
      object C = new object();

      //I need to change Object A to point to the contents of Object C only using Object B
      Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

      i.e

      //in main or whatever
      //create object A
      object A = new object();
      someFunction(A) ;
      .
      .
      .
      private void someFunction(re f object B)
      {
      object C = new object();
      B = C;
      }


      Now A has been changed to C through B

      Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

      -mwalts

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by mwalts
        Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

        i.e

        //in main or whatever
        //create object A
        object A = new object();
        someFunction(A) ;
        .
        .
        .
        private void someFunction(re f object B)
        {
        object C = new object();
        B = C;
        }


        Now A has been changed to C through B

        Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

        -mwalts
        Well, now A has been set to the contents of C, but changing A would not also change C. They don't occupy the same memory, they are still just copies of one another.

        If you want to do memory manipulation you have to make the jump into unmanaged code (unsafe code, check msdn on it) and that gets real nasty real fast.

        Comment

        • MiddleTommy
          New Member
          • May 2007
          • 5

          #5
          The real situation is I have a field from a datarow that I put into a collection.
          Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.

          Comment

          • MiddleTommy
            New Member
            • May 2007
            • 5

            #6
            It would be easy if only C# supported pointers to Reference Types

            Comment

            • mwalts
              New Member
              • May 2007
              • 38

              #7
              Originally posted by MiddleTommy
              It would be easy if only C# supported pointers to Reference Types
              Yeah, but direct memory manipulation would kinda defeat the whole managed sandbox concept period.

              For normal reference types you would be able to change their values from the fields located in your collection... for boxed value types and immutable reference types (which are of course the most common in DataRow's) I'm not sure what the behavior would be. Probably won't work since you would have to unbox it to change the value... But it might be worth a try to just change the collections value and see what you get

              You might have to put the DataRow into the collection instead of the individual field (or some such work around)

              Good luck,

              -mwalts

              Comment

              • oohay251
                New Member
                • May 2007
                • 27

                #8
                Originally posted by MiddleTommy
                The real situation is I have a field from a datarow that I put into a collection.
                Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.
                Why don't you want to store a reference to the object itself?

                Also, you could implement an event-type system to update the datarow when the value in your collection changes.

                Comment

                • MiddleTommy
                  New Member
                  • May 2007
                  • 5

                  #9
                  Yeah, I had to put the datarow into the collection

                  I suppose for more generic answer
                  if you encapsulate a object into another object you can pass the outer object and then change the inner object without loosing the reference?

                  or something like that

                  Comment

                  • MiddleTommy
                    New Member
                    • May 2007
                    • 5

                    #10
                    class WrapperObject
                    { public TrackedObject obj }

                    //now you can do this.
                    WrapperObject A = new WrapperObject() ;
                    A.obj = new TrackedObject() ;
                    WrapperObject B = new WrapperObject() ;

                    TrackedObject C = new TrackedObject() ;
                    B.obj = C;

                    //now A.obj = C

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      Anything created with the "new" keyword will be passed by "reference" automatically.
                      So if your function takes in as an argument an object,
                      any actions done on that object withen the function are actually done to the object that was passed in.

                      I used to do this when I needed a cheap way to get one form to talk to another, I would pass a textbox into the contrustor. (Note: don't actually pass textboxes and stuff like that)

                      Comment

                      Working...