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.
  • 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
    Maximilien
    wrote on last edited by
    #2

    by value.

    This signature was proudly tested on animals.

    S 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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      It's passed by the lower intestinal tract.

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

      My blog | My articles

      B M 2 Replies 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

        B Offline
        B Offline
        Big Daddy Farang
        wrote on last edited by
        #4

        Ah, so it is by value.

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

        C S 2 Replies Last reply
        0
        • B Big Daddy Farang

          Ah, so it is by value.

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

          C Offline
          C Offline
          Chris Meech
          wrote on last edited by
          #5

          I think by volume. :)

          Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

          B P 2 Replies Last reply
          0
          • C Chris Meech

            I think by volume. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

            B Offline
            B Offline
            Big Daddy Farang
            wrote on last edited by
            #6

            Although not one of the choices in the original post, I think it's a good answer given that the OP is a load of, umm, excrement.

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

            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.

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

              What's the point of your post? :|

              "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

              S 1 Reply Last reply
              0
              • C Chris Meech

                I think by volume. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]

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

                Chris Meech wrote:

                think by volume.

                ...and alot of it too :laugh:

                "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
                • M Maximilien

                  by value.

                  This signature was proudly tested on animals.

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

                  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 1 Reply Last reply
                  0
                  • P Paul Conrad

                    What's the point of your post? :|

                    "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

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

                    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 P 2 Replies Last reply
                    0
                    • 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
                                          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