I have a class PageMaster
, for which I instantiate several instances, including one called EmptyPage, which I never write to (only read from).
I have other class instances such as IndexPage, PrivacyPage et al. which have different properties set.
In my program (code below), when I modify any one of these class instances, every single other instance is also being modified in the exact same way.
Log3("Resetting special pages")
Log3("Setting pages equal to EmptyPage")
IndexPage = EmptyPage
PrivacyPage = EmptyPage
ContactPage = EmptyPage
AboutPage = EmptyPage
SiteIndexPage = EmptyPage
LogSpecialPages()
Log3("Setting flag IsIndexPage")
IndexPage.IsIndexPage = True
LogSpecialPages()
Log3("Setting flag isPrivacyPage")
PrivacyPage.IsPrivacyPage = True
LogSpecialPages()
Log3("Setting flag isContactPage")
ContactPage.IsContactPage = True
LogSpecialPages()
Log3("Setting flag isAboutPage")
AboutPage.IsAboutPage = True
LogSpecialPages()
Log3("Setting flag isSiteIndexPage")
SiteIndexPage.IsSiteIndexPage = True
LogSpecialPages()
Log3("Reset finished")
In this actual code from my program, when I perform the instruction IndexPage.IsIndexPage = True
every single instance of Page - PrivacyPage, ContactPage, et al. are all modified - not just IndexPage. The same thing happens with the others. When I modify one instance, all other instances have string and/or boolean variable values changed.
(To do the logging, I serialize the class instances with JSONConvert and write it to an output.)
I can't understand why every single instance is being modified. This is how they are instantiated, inside my form's class:
Dim IndexPage As New PageMaster
Dim PrivacyPage As New PageMaster
Dim ContactPage As New PageMaster
Dim AboutPage As New PageMaster
Dim SiteIndexPage As New PageMaster
Dim EmptyPage As New PageMaster
The only possible explanation I can come up with is that when I say, IndexPage = EmptyPage
, and assign all the others equal to EmptyPage, that they somehow lose their individual identities. That doesn't make sense to me, though, because I've never had anything like this happen.
The class is very simple, with a few string and boolean properties.
I then tried this simple modification. Instead of IndexPage = EmptyPage
I used IndexPage = New PageMaster
and it worked correctly.
This seems to confirm that, somehow, setting them all equal to EmptyPage is confusing their identifies.
Why do their identities become confused, causing any changes to one instance to be made to all?
It's like somehow when I say IndexPage = EmptyPage
that it's just creating a symbolic reference from one to the other, rather than the behavior I expected, which was to set all the properties of one equal to the properties of the other.
UPDATE
After Jimi's comment confirming what I suspected to be the case, I decided to do a little test. I just put a button on my test app and added this code in its click event:
' PART 1: STRING
Dim name1 As String = "Bob"
Dim name2 As String = "Larry"
name2 = name1
name2 = "Fred"
MsgBox(name1)
'PART 2: CUSTOM CLASS
Dim Bob As New Person
Bob.Name = "Bob"
Dim Larry As New Person
Larry.Name = "Larry"
Larry = Bob
Larry.Name = "Hubert"
MsgBox(Bob.Name)
In the first example, we get the response of "Bob", which is what I would have expected.
In the second msgbox, we don't get "Bob". Instead, we get "Hubert", because class instance Bob now points to Larry, and Larry's name was changed to "Hubert".
This prompts me to ask these questions:
- Why is the behavior is so completely different using String versus a class of our own creation?
- Can a different behavior be forced with custom classes? Or is there another simple way to "Copy" one instance of a class over another?
- Does this presumably carry over into C#, which I also use?