Why changes to a private variable is also done to a public one?











up vote
5
down vote

favorite
1












I have a public variable Public AssetFamilyCollection As New Collection which is a collection of the classe AssetFamily I've created.



Within a sub, I create an AssetFamily instance with Dim familyChosen As AssetFamily.
Then when I've identified the AssetFamily I want in the collection I do Set familyChosen = AssetFamilyCollection(i)



At some point, I make changes on familyChosen property and I noticed that those changes have also been done to AssetFamilyCollection(i)



I thought familyChosen was a private variable, a copy from AssetFamilyCollection(i) and only exists inside the sub. Apparently not.



Why the public and private variable are impacted by the changes and not the private one in the sub ?



Thanks !










share|improve this question




























    up vote
    5
    down vote

    favorite
    1












    I have a public variable Public AssetFamilyCollection As New Collection which is a collection of the classe AssetFamily I've created.



    Within a sub, I create an AssetFamily instance with Dim familyChosen As AssetFamily.
    Then when I've identified the AssetFamily I want in the collection I do Set familyChosen = AssetFamilyCollection(i)



    At some point, I make changes on familyChosen property and I noticed that those changes have also been done to AssetFamilyCollection(i)



    I thought familyChosen was a private variable, a copy from AssetFamilyCollection(i) and only exists inside the sub. Apparently not.



    Why the public and private variable are impacted by the changes and not the private one in the sub ?



    Thanks !










    share|improve this question


























      up vote
      5
      down vote

      favorite
      1









      up vote
      5
      down vote

      favorite
      1






      1





      I have a public variable Public AssetFamilyCollection As New Collection which is a collection of the classe AssetFamily I've created.



      Within a sub, I create an AssetFamily instance with Dim familyChosen As AssetFamily.
      Then when I've identified the AssetFamily I want in the collection I do Set familyChosen = AssetFamilyCollection(i)



      At some point, I make changes on familyChosen property and I noticed that those changes have also been done to AssetFamilyCollection(i)



      I thought familyChosen was a private variable, a copy from AssetFamilyCollection(i) and only exists inside the sub. Apparently not.



      Why the public and private variable are impacted by the changes and not the private one in the sub ?



      Thanks !










      share|improve this question















      I have a public variable Public AssetFamilyCollection As New Collection which is a collection of the classe AssetFamily I've created.



      Within a sub, I create an AssetFamily instance with Dim familyChosen As AssetFamily.
      Then when I've identified the AssetFamily I want in the collection I do Set familyChosen = AssetFamilyCollection(i)



      At some point, I make changes on familyChosen property and I noticed that those changes have also been done to AssetFamilyCollection(i)



      I thought familyChosen was a private variable, a copy from AssetFamilyCollection(i) and only exists inside the sub. Apparently not.



      Why the public and private variable are impacted by the changes and not the private one in the sub ?



      Thanks !







      vba excel-vba excel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 9 at 19:34









      Community

      11




      11










      asked Mar 17 '17 at 17:21









      TmSmth

      14613




      14613
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          No it doesn't work like that.



          The variable familyChosen is actually a reference to the same object as the array element is referring.



          So you can modify that object either through that reference or through the array element.






          share|improve this answer






























            up vote
            3
            down vote















            • This is the problem about the Basic Type and the Object Type.



              You may need to know the diffenerce between the Deep Copy and
              Shallow Copy.








            • When the code "Dim familyChosen As AssetFamily" executed, a new
              “AssetFamily” was created.



              But, when "Set familyChosen = AssetFamilyCollection(i)" executed,
              nothing was created, you just made the familyChosen pointed to the
              “AssetFamily” that already existed.








            • So, if you want a "copy" but not a "pointer", you can write a
              function in the class “AssetFamily”, such as “Clone”, to create a new
              "AssetFamily" and make it the same as the old one.



              Then, you can write something like this:



              Set familyChosen = AssetFamilyCollection(i).Clone()








            share|improve this answer






























              up vote
              2
              down vote













              It's becuase of



              Set familyChosen = AssetFamilyCollection(i)


              'familyChosen' is now a reference to 'AssetFamilyCollection(i)'



              Any changes to one instance will update the other as they are both referencing the same object.






              share|improve this answer



















              • 1




                Formally it's a reference to the same object as that referred to by the array element.
                – Bathsheba
                Mar 17 '17 at 17:27











              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42863653%2fwhy-changes-to-a-private-variable-is-also-done-to-a-public-one%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              6
              down vote



              accepted










              No it doesn't work like that.



              The variable familyChosen is actually a reference to the same object as the array element is referring.



              So you can modify that object either through that reference or through the array element.






              share|improve this answer



























                up vote
                6
                down vote



                accepted










                No it doesn't work like that.



                The variable familyChosen is actually a reference to the same object as the array element is referring.



                So you can modify that object either through that reference or through the array element.






                share|improve this answer

























                  up vote
                  6
                  down vote



                  accepted







                  up vote
                  6
                  down vote



                  accepted






                  No it doesn't work like that.



                  The variable familyChosen is actually a reference to the same object as the array element is referring.



                  So you can modify that object either through that reference or through the array element.






                  share|improve this answer














                  No it doesn't work like that.



                  The variable familyChosen is actually a reference to the same object as the array element is referring.



                  So you can modify that object either through that reference or through the array element.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 17 '17 at 17:33

























                  answered Mar 17 '17 at 17:24









                  Bathsheba

                  173k27244367




                  173k27244367
























                      up vote
                      3
                      down vote















                      • This is the problem about the Basic Type and the Object Type.



                        You may need to know the diffenerce between the Deep Copy and
                        Shallow Copy.








                      • When the code "Dim familyChosen As AssetFamily" executed, a new
                        “AssetFamily” was created.



                        But, when "Set familyChosen = AssetFamilyCollection(i)" executed,
                        nothing was created, you just made the familyChosen pointed to the
                        “AssetFamily” that already existed.








                      • So, if you want a "copy" but not a "pointer", you can write a
                        function in the class “AssetFamily”, such as “Clone”, to create a new
                        "AssetFamily" and make it the same as the old one.



                        Then, you can write something like this:



                        Set familyChosen = AssetFamilyCollection(i).Clone()








                      share|improve this answer



























                        up vote
                        3
                        down vote















                        • This is the problem about the Basic Type and the Object Type.



                          You may need to know the diffenerce between the Deep Copy and
                          Shallow Copy.








                        • When the code "Dim familyChosen As AssetFamily" executed, a new
                          “AssetFamily” was created.



                          But, when "Set familyChosen = AssetFamilyCollection(i)" executed,
                          nothing was created, you just made the familyChosen pointed to the
                          “AssetFamily” that already existed.








                        • So, if you want a "copy" but not a "pointer", you can write a
                          function in the class “AssetFamily”, such as “Clone”, to create a new
                          "AssetFamily" and make it the same as the old one.



                          Then, you can write something like this:



                          Set familyChosen = AssetFamilyCollection(i).Clone()








                        share|improve this answer

























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote











                          • This is the problem about the Basic Type and the Object Type.



                            You may need to know the diffenerce between the Deep Copy and
                            Shallow Copy.








                          • When the code "Dim familyChosen As AssetFamily" executed, a new
                            “AssetFamily” was created.



                            But, when "Set familyChosen = AssetFamilyCollection(i)" executed,
                            nothing was created, you just made the familyChosen pointed to the
                            “AssetFamily” that already existed.








                          • So, if you want a "copy" but not a "pointer", you can write a
                            function in the class “AssetFamily”, such as “Clone”, to create a new
                            "AssetFamily" and make it the same as the old one.



                            Then, you can write something like this:



                            Set familyChosen = AssetFamilyCollection(i).Clone()








                          share|improve this answer
















                          • This is the problem about the Basic Type and the Object Type.



                            You may need to know the diffenerce between the Deep Copy and
                            Shallow Copy.








                          • When the code "Dim familyChosen As AssetFamily" executed, a new
                            “AssetFamily” was created.



                            But, when "Set familyChosen = AssetFamilyCollection(i)" executed,
                            nothing was created, you just made the familyChosen pointed to the
                            “AssetFamily” that already existed.








                          • So, if you want a "copy" but not a "pointer", you can write a
                            function in the class “AssetFamily”, such as “Clone”, to create a new
                            "AssetFamily" and make it the same as the old one.



                            Then, you can write something like this:



                            Set familyChosen = AssetFamilyCollection(i).Clone()









                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 11 at 6:42

























                          answered Mar 17 '17 at 17:47









                          MoonLord

                          312




                          312






















                              up vote
                              2
                              down vote













                              It's becuase of



                              Set familyChosen = AssetFamilyCollection(i)


                              'familyChosen' is now a reference to 'AssetFamilyCollection(i)'



                              Any changes to one instance will update the other as they are both referencing the same object.






                              share|improve this answer



















                              • 1




                                Formally it's a reference to the same object as that referred to by the array element.
                                – Bathsheba
                                Mar 17 '17 at 17:27















                              up vote
                              2
                              down vote













                              It's becuase of



                              Set familyChosen = AssetFamilyCollection(i)


                              'familyChosen' is now a reference to 'AssetFamilyCollection(i)'



                              Any changes to one instance will update the other as they are both referencing the same object.






                              share|improve this answer



















                              • 1




                                Formally it's a reference to the same object as that referred to by the array element.
                                – Bathsheba
                                Mar 17 '17 at 17:27













                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              It's becuase of



                              Set familyChosen = AssetFamilyCollection(i)


                              'familyChosen' is now a reference to 'AssetFamilyCollection(i)'



                              Any changes to one instance will update the other as they are both referencing the same object.






                              share|improve this answer














                              It's becuase of



                              Set familyChosen = AssetFamilyCollection(i)


                              'familyChosen' is now a reference to 'AssetFamilyCollection(i)'



                              Any changes to one instance will update the other as they are both referencing the same object.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 17 '17 at 17:27

























                              answered Mar 17 '17 at 17:25









                              Imran Saeed

                              3,08111026




                              3,08111026








                              • 1




                                Formally it's a reference to the same object as that referred to by the array element.
                                – Bathsheba
                                Mar 17 '17 at 17:27














                              • 1




                                Formally it's a reference to the same object as that referred to by the array element.
                                – Bathsheba
                                Mar 17 '17 at 17:27








                              1




                              1




                              Formally it's a reference to the same object as that referred to by the array element.
                              – Bathsheba
                              Mar 17 '17 at 17:27




                              Formally it's a reference to the same object as that referred to by the array element.
                              – Bathsheba
                              Mar 17 '17 at 17:27


















                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42863653%2fwhy-changes-to-a-private-variable-is-also-done-to-a-public-one%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues