In the following code…
[code]Dim a As new SomeObject()
Dim b As SomeObject
b = a
[/code]
How can a check if b is pointing to the same object than a?
e.g. if a and b are references to the same object then do something…
In the following code…
[code]Dim a As new SomeObject()
Dim b As SomeObject
b = a
[/code]
How can a check if b is pointing to the same object than a?
e.g. if a and b are references to the same object then do something…
Thanks Eli.
Ok, I’m not sure what I’m doing wrong then… in this code, shouldn’t the message box display?
Dim xdoc as new XmlDocument("<a><b></b><c></c></a>")
Dim n1 As XmlNode
Dim n2 As XmlNode
n1 = xdoc.Child(0)
n2 = xdoc.Child(0)
if n1 is n2 then
msgbox "Why isn't this displaying?"
end if
[quote=123124:@Alwyn Bester] if n1 is n2 then
msgbox “Why isn’t this displaying?”
end if[/quote]
if n1 isA n2 then
msgbox "Why isn't this displaying?"
end if
isA
instead of is
will do it
The problem is, IsA checks if the two references points to the same type of object (e.g. XmlNode). I need to check if they are pointing to the exact same object instance. “Is” is the right operator to do this.
I suspect the problem might be that xdoc.Child(0) doesn’t return an existing XmlNode instance , but creates a new instance each time? (A Xojo engineer will have to confirm if this is so).
The expression “xdoc.Child(0) Is xdoc.Child(0)” will then always be false, because two different instances are compared.
Dim xdoc as new XmlDocument("<a><b></b><c></c></a>")
if xdoc.Child(0) is xdoc.Child(0) then
msgbox "Shouldn't this message box display?"
end if
Language Reference States: Returns the child XMLNode at the position denoted by Index. Index is zero-based.
Appears it “should” work
[quote=123136:@Alwyn Bester]The problem is, IsA checks if the two references points to the same type of object (e.g. XmlNode). I need to check if they are pointing to the exact same object instance. “Is” is the right operator to do this.
I suspect the problem might be that xdoc.Child(0) doesn’t return an existing XmlNode instance , but creates a new instance each time? (A Xojo engineer will have to confirm if this is so).
The expression “xdoc.Child(0) Is xdoc.Child(0)” will then always be false, because two different instances are compared.
[code]
Dim xdoc as new XmlDocument("")
if xdoc.Child(0) is xdoc.Child(0) then
msgbox “Shouldn’t this message box display?”
end if
[/code][/quote]
What about using a common name reference :
[code] Dim xdoc as new XmlDocument("")
xdoc.DocumentElement.name = “mydoc”
Dim n1 As XmlNode
Dim n2 As XmlNode
n1 = xdoc.Child(0)
n2 = xdoc.Child(0)
if n1.Name = n2.Name then
msgbox "Point to "+xdoc.DocumentElement.name
end if[/code]
I absolutely agree, A Xojo engineer will have to confirm this.
I also at first thought of using the Name property. The problem is however, there can be multiple children with the same name…
Dim xdoc as new XmlDocument("<a><b></b><c></c><c></c></a>")
I need a 100% gaurentee that it is the same instance, and comparing the Names could potentially make a wrong match.
How about giving your classes a unique ID and simply compare the IDs?
I did eventually find a workaround for what I needed to do (something similar to your suggestion Beatrix), but are now just wondering out of curiosity why
if xdoc.Child(0) is xdoc.Child(0) then
evaluates to false instead of true.
Because it is returning a different instance
[code] Dim xdoc as new XmlDocument("")
dim t1 As Object = xdoc.Child(0)
dim t2 As Object = xdoc.Child(0)
break[/code]
In the debugger you can see t1 and t2 have different addresses
How do I see the addresses in the debugger Will? I’m only seeing the normal properties…
…and now I am even more confused… if they are different instances… how come when I change a value in n1, the changes reflects in n2?
Dim xdoc as new XmlDocument("<a><b></b><c></c></a>")
Dim n1 As XmlNode
Dim n2 As XmlNode
n1 = xdoc.FirstChild.Child(0)
n2 = xdoc.FirstChild.Child(0)
msgbox n2.Name
n1.Name = "d"
msgbox n2.Name // <--- this displays d, which proves n2 can be updated via n1... implying they are referencing the same instances
// but why then is
if n1 is n2 then // equal to false
MsgBox "This message should show" // but it doesn't
end if
PS. I made a mistake in my previous code, should have been xdoc.FIRSTCHILD.Child(0)
Something is definitely weird.
If you try the is operator with (e.g.) Window like suggested in the docs it works, and the ObjectID shown in debugger is the same (btw the ObjectID display can be enabled in the Preferences).
So it could be something weird with the XMLNode class.
Nice, never knew about this. Just goes to show… you can work with a tool for years and still learn new tricks
There is definitely something strange about the XmlNode class. The addresses are different, but updates to properties reflects in both.
Something like different pointers pointing to the same place. Hello Duckies. Bye Duckies.
The trick here is that the XML.FirstChild.Child(0) returns a new object that refers to the same underlying data
So while they refer to the same data they are NOT the same reference
However IF you do
n1 = xdoc.FirstChild.Child(0)
n2 = n1
now N1 and N2 definitely ARe two references to the same XML elements
I think this is not the correct behavior. Child() should return always the same object.
Its been this way probably since the day the XML plugin first appeared (like 10 years ago)