Why changes to a private variable is also done to a public one?
up vote
5
down vote
favorite
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
add a comment |
up vote
5
down vote
favorite
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
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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
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
vba excel-vba excel
edited Jul 9 at 19:34
Community♦
11
11
asked Mar 17 '17 at 17:21
TmSmth
14613
14613
add a comment |
add a comment |
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.
add a comment |
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()
add a comment |
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.
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 17 '17 at 17:33
answered Mar 17 '17 at 17:24
Bathsheba
173k27244367
173k27244367
add a comment |
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
edited Nov 11 at 6:42
answered Mar 17 '17 at 17:47
MoonLord
312
312
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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