Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Assign By Reference

Assign By Reference

Scheduled Pinned Locked Moved C#
csharpquestiontutorial
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thenutz72
    wrote on last edited by
    #1

    Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

    P L K B 5 Replies Last reply
    0
    • T thenutz72

      Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Ummm... what? It's hard to read that code; put it in pre tags (select it and click code block).

      thenutz72 wrote:

      It's like you can have reference behaviour through functions that you cannot have WITHIN the function.

      C# (.net) is all about references.

      thenutz72 wrote:

      is forever unlinked

      I don't know what that's supposed to mean.

      thenutz72 wrote:

      way to do something more like

      No, that's silly.

      1 Reply Last reply
      0
      • T thenutz72

        Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        This really doesn't make any sense to me. What does this even mean?

        thenutz72 wrote:

        m_Myclass = ref objIn;

        1 Reply Last reply
        0
        • T thenutz72

          Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          thenutz72 wrote:

          It's like you can have reference behaviour through functions that you cannot have WITHIN the function.

          Pretty much everything is done by reference:

          MyObject foo = new MyObject();

          foo is a reference to the object created by the constructor. When it gets used it is automatically de-referenced, see this[^] for the c# boxing and unboxing model. When you pass an object to a method it is passed as a reference by default, you have to tell the method if you want it passed by value. If you need pointers you have to use unmanagedunsafe code, where pointers are available, but in C# you rarely need it. I've being doing this for 10 years, and the only time I had to use pointers was during some interop work I was doing. Oh and a another time after I'd just graduated using c++ and didn't know how C# really worked, which was a bit embarrasing :-) Finally, please tag up your code with <pre> , it makes it easier to read. Otherwise you'll get responses of varying degrees of snottiness. [edit]: When I said unmanaged code, I of course meant unsafe code. Better have a nice nap and a cup of tea, I'm getting old!

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]

          modified on Saturday, October 9, 2010 1:03 PM

          T 1 Reply Last reply
          0
          • T thenutz72

            Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Unsafe code?

            unsafe
            {
            int i = 1;
            Console.WriteLine((long)&i);
            }

            Explained on MSDN[^]

            I are Troll :suss:

            1 Reply Last reply
            0
            • K Keith Barrow

              thenutz72 wrote:

              It's like you can have reference behaviour through functions that you cannot have WITHIN the function.

              Pretty much everything is done by reference:

              MyObject foo = new MyObject();

              foo is a reference to the object created by the constructor. When it gets used it is automatically de-referenced, see this[^] for the c# boxing and unboxing model. When you pass an object to a method it is passed as a reference by default, you have to tell the method if you want it passed by value. If you need pointers you have to use unmanagedunsafe code, where pointers are available, but in C# you rarely need it. I've being doing this for 10 years, and the only time I had to use pointers was during some interop work I was doing. Oh and a another time after I'd just graduated using c++ and didn't know how C# really worked, which was a bit embarrasing :-) Finally, please tag up your code with <pre> , it makes it easier to read. Otherwise you'll get responses of varying degrees of snottiness. [edit]: When I said unmanaged code, I of course meant unsafe code. Better have a nice nap and a cup of tea, I'm getting old!

              Sort of a cross between Lawrence of Arabia and Dilbert.[^]

              modified on Saturday, October 9, 2010 1:03 PM

              T Offline
              T Offline
              thenutz72
              wrote on last edited by
              #6

              Thanks Kieth, But in my code examples you'll see the ref keyword does act like a pointer to an object. Where as the assignment operator just assigns a reference. In the former, there are two variables with the same address. The latter, two variables that only point to the same reference. Use of the new keyword in each scenario will confirm the difference. I just find it odd that a function parameter can make use of pointers to objects but there's no way to duplicate that behaviour inside of a function. I was able to use unsafe code and pointers to primitive types to share addresses across multple variables. But was unable to do the same with a class/object instance.

              P K 2 Replies Last reply
              0
              • T thenutz72

                Thanks Kieth, But in my code examples you'll see the ref keyword does act like a pointer to an object. Where as the assignment operator just assigns a reference. In the former, there are two variables with the same address. The latter, two variables that only point to the same reference. Use of the new keyword in each scenario will confirm the difference. I just find it odd that a function parameter can make use of pointers to objects but there's no way to duplicate that behaviour inside of a function. I was able to use unsafe code and pointers to primitive types to share addresses across multple variables. But was unable to do the same with a class/object instance.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                You still haven't explained your question clearly.

                1 Reply Last reply
                0
                • T thenutz72

                  Thanks Kieth, But in my code examples you'll see the ref keyword does act like a pointer to an object. Where as the assignment operator just assigns a reference. In the former, there are two variables with the same address. The latter, two variables that only point to the same reference. Use of the new keyword in each scenario will confirm the difference. I just find it odd that a function parameter can make use of pointers to objects but there's no way to duplicate that behaviour inside of a function. I was able to use unsafe code and pointers to primitive types to share addresses across multple variables. But was unable to do the same with a class/object instance.

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  First, in your code, the ref keyword is redundant, the reference pointer is passed round on objects by default, not the value itself. Second, I'm still really not clear on your question.

                  private MyClass
                  public void MyFunction(MyClass objIn)
                  {
                  m_MyClass = objIn;
                  }

                  public void SomeOtherFunction()
                  {
                  m_MyClass = new MyClass() //*Now the calling function's object is forever unlinked*
                  // Actually, it isn't just unlinked, the m_MyClass reference is replaced with the one
                  // you have just instantiated, The original object is orphaned and ready up for Garbage
                  // collection as no reference to it exists any more.
                  }

                  public void Main()
                  {
                  MyFunction(new MyClass());
                  SomeOtherFunction();
                  }

                  Which language are you used to using and what exactly are you trying to achieve? If you need pointers you are probably going about things the wrong way in C#.

                  Sort of a cross between Lawrence of Arabia and Dilbert.[^]

                  1 Reply Last reply
                  0
                  • T thenutz72

                    Is there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.

                    B Offline
                    B Offline
                    Bernhard Hiller
                    wrote on last edited by
                    #9

                    I am not sure if I understand what you want to accomplish. I guess you could try the out keyword instead of the ref keyword.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups