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. Other Discussions
  3. The Weird and The Wonderful
  4. Pass by value and pass by reference

Pass by value and pass by reference

Scheduled Pinned Locked Moved The Weird and The Wonderful
24 Posts 15 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.
  • B Big Daddy Farang

    Ah, so it is by value.

    BDF A learned fool is more a fool than an ignorant fool. -- Moliere

    S Offline
    S Offline
    Sherin Iranimose
    wrote on last edited by
    #11

    by the lower intestinal tract.

    Then how its by value??? Once its passed, you wont get the original....... :laugh:

    EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question

    1 Reply Last reply
    0
    • S Sherin Iranimose

      After the function call its printing 30, That is the matter :)

      EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question

      D Offline
      D Offline
      darkelv
      wrote on last edited by
      #12

      Sherin Iranimose wrote:

      After the function call its printing 30

      So, that's the horror part? *look at forum title* *Disappointed*

      1 Reply Last reply
      0
      • S Sherin Iranimose

        namespace SampleCSharp { public class MyClass { public int myVar; } public class SampleClass { private void SampleClass_Load(object sender, EventArgs e) { MyClass objMyClass = new MyClass(); objMyClass.myVar = 10; ChangeMyVar(objMyClass);//**IS THIS PASS BY VALUE OR PASS BY REFERENCE** } public void ChangeMyVar(MyClass objMyClass) { objMyClass.myVar = 30; } } }

        EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #13

        this looks like a programming question. On top of this forum it's said in big bold red letters: Do not post programming questions in this forum... If it is a question, move it to the C# forum. Btw: objects (including strings) are passed by reference to my knowledge. You should be able to find this on msdn somewhere.

        V.
        Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

        CPalliniC 1 Reply Last reply
        0
        • V V 0

          this looks like a programming question. On top of this forum it's said in big bold red letters: Do not post programming questions in this forum... If it is a question, move it to the C# forum. Btw: objects (including strings) are passed by reference to my knowledge. You should be able to find this on msdn somewhere.

          V.
          Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #14

          V. wrote:

          objects (including strings) are passed by reference to my knowledge

          Nope. All parameters are passed by value. you should use the ref keyword to pass a parameter by reference. BTW: passing by value an object implies that called function can actually change object's internal state. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          J 1 Reply Last reply
          0
          • S Sherin Iranimose

            After the function call its printing 30, That is the matter :)

            EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question

            P Offline
            P Offline
            Paul Conrad
            wrote on last edited by
            #15

            For some reason, I sense you are asking a programming question. That is a big NO in this particular forum.

            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

            1 Reply Last reply
            0
            • S Sherin Iranimose

              namespace SampleCSharp { public class MyClass { public int myVar; } public class SampleClass { private void SampleClass_Load(object sender, EventArgs e) { MyClass objMyClass = new MyClass(); objMyClass.myVar = 10; ChangeMyVar(objMyClass);//**IS THIS PASS BY VALUE OR PASS BY REFERENCE** } public void ChangeMyVar(MyClass objMyClass) { objMyClass.myVar = 30; } } }

              EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #16

              This appears to be C# code. Classes are passed by reference by design.

              Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

              1 Reply Last reply
              0
              • CPalliniC CPallini

                V. wrote:

                objects (including strings) are passed by reference to my knowledge

                Nope. All parameters are passed by value. you should use the ref keyword to pass a parameter by reference. BTW: passing by value an object implies that called function can actually change object's internal state. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                J Offline
                J Offline
                Joe Woodbury
                wrote on last edited by
                #17

                This is incorrect. Classes are passed by reference, Structs are passed by value.

                CPallini wrote:

                BTW: passing by value an object implies that called function can actually change object's internal state. [Smile]

                This means nothing of the sort. Passing by value simply means that an object in a method is distinct from the original object and any changes made to that object are not reflected in the original.

                Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                CPalliniC 1 Reply Last reply
                0
                • J Joe Woodbury

                  This is incorrect. Classes are passed by reference, Structs are passed by value.

                  CPallini wrote:

                  BTW: passing by value an object implies that called function can actually change object's internal state. [Smile]

                  This means nothing of the sort. Passing by value simply means that an object in a method is distinct from the original object and any changes made to that object are not reflected in the original.

                  Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #18

                  You are wrong. Whenever you pass an object, a reference to the object's instance is passed by value. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • S Sherin Iranimose

                    namespace SampleCSharp { public class MyClass { public int myVar; } public class SampleClass { private void SampleClass_Load(object sender, EventArgs e) { MyClass objMyClass = new MyClass(); objMyClass.myVar = 10; ChangeMyVar(objMyClass);//**IS THIS PASS BY VALUE OR PASS BY REFERENCE** } public void ChangeMyVar(MyClass objMyClass) { objMyClass.myVar = 30; } } }

                    EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

                    K Offline
                    K Offline
                    KarstenK
                    wrote on last edited by
                    #19

                    I guess it gets passed by reference because it is C#. :rolleyes: In C++ by value.

                    Greetings from Germany

                    1 Reply Last reply
                    0
                    • S Sherin Iranimose

                      namespace SampleCSharp { public class MyClass { public int myVar; } public class SampleClass { private void SampleClass_Load(object sender, EventArgs e) { MyClass objMyClass = new MyClass(); objMyClass.myVar = 10; ChangeMyVar(objMyClass);//**IS THIS PASS BY VALUE OR PASS BY REFERENCE** } public void ChangeMyVar(MyClass objMyClass) { objMyClass.myVar = 30; } } }

                      EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

                      M Offline
                      M Offline
                      Mitendra Anand
                      wrote on last edited by
                      #20

                      Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. So it is actually a PASS - BY - REFERENCE Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. Happy Coding! Mitendra

                      J 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        It's passed by the lower intestinal tract.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        M Offline
                        M Offline
                        Muigai Mwaura
                        wrote on last edited by
                        #21

                        I'm cracking up :laugh: I'm shedding tears :(( the people I work with are looking at me like I'm crazy.

                        1 Reply Last reply
                        0
                        • M Mitendra Anand

                          Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. So it is actually a PASS - BY - REFERENCE Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. Happy Coding! Mitendra

                          J Offline
                          J Offline
                          jon_175
                          wrote on last edited by
                          #22

                          The object is passed by value, but the what is actually passed since it is a reference object is a pointer to the object. So, the "value" that is actually passed is the pointer not the object and therefore any changes made through the pointer change the original object. What you can't change is the pointer itself. That is the "value". :)

                          M 1 Reply Last reply
                          0
                          • J jon_175

                            The object is passed by value, but the what is actually passed since it is a reference object is a pointer to the object. So, the "value" that is actually passed is the pointer not the object and therefore any changes made through the pointer change the original object. What you can't change is the pointer itself. That is the "value". :)

                            M Offline
                            M Offline
                            Mitendra Anand
                            wrote on last edited by
                            #23

                            You are right Jon. I see your point.

                            1 Reply Last reply
                            0
                            • S Sherin Iranimose

                              So If I print the value anywhere inside the Load function will only print 10. But it is not.... Its 30 after function call.

                              EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question

                              B Offline
                              B Offline
                              BillW33
                              wrote on last edited by
                              #24

                              When the class object is passed into ChangeMyVar(MyClass objMyClass) it is passed by value. Saying it another way: a copy of the object is made and then used by the ChangeMyVar method. Since this is a copy any changes to it do not affect the original. Bill W

                              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