Page 1 of 1

[Issue] Basic IS operator

Posted: Tue May 20, 2025 11:49 pm
by chris-barry
Why does the expression:

Code: Select all

ThisComponent.sheets(0) is ThisComponent.sheets(0)
evaluate to False?

Re: The "is" operator

Posted: Wed May 21, 2025 12:05 am
by floris v
See https://help.libreoffice.org/25.2/en-US ... ystem=UNIX for how the is operator works. This is very abstract stuff. My guess is that when you call ThisComponent.sheets(0), a pointer and object is created referencing that sheet. In your ThisComponent.sheets(0) is ThisComponent.sheets(0) that's done twice, resulting in two objects referencing the same sheet. But because they're different objects, the is operator returns False.
You should probably use one variable dim'd as object and one as variant/pointer as in the article in the link.

Re: The "is" operator

Posted: Wed May 21, 2025 12:12 pm
by chris-barry
I had already seen that article.
The initial sentence, "Tests if two Basic variables refer to the same object instance.", seems to imply, at least to me, that two variables, each declared as "object" and assigned to the same actual object should have returned True.

I wanted to use this to find the value of the index for a sheet by looping through sheets until I got a match. Of course I can compare CodeName instead, the extra cost of two variable name dereferences and a string comparison is trivial to the point of irrelevance. I was curious, not so much about the implementation details as about the reasons for the original design decision, since in its current form it is so limited that I cannot think of a situation in which I would find it useful.

Re: The "is" operator

Posted: Wed May 21, 2025 12:41 pm
by chris-barry
Immediately after posting my previous message I noticed a StackOverflow telling me how to solve my original problem (RangeAddress.sheet), so my use-case was invalid. Nonetheless, I still think that this is a legitimate question.

Re: The "is" operator

Posted: Wed May 21, 2025 2:52 pm
by floris v
You should probably use a variant variable to loop through all sheets instead of an object variable. The variant will directly point at the sheet, while what you call the object is basically a pointer to an object variable that points at the sheet. You can have several object variables pointing at the same sheet, but they will still be different objects at different locations in memory.

Re: The "is" operator

Posted: Wed May 21, 2025 5:39 pm
by chris-barry
Is this what you mean?

Code: Select all

sub test_2()
	dim sht_1 as variant
	dim sht_2 as variant
	
	sht_1 = ThisComponent.sheets.getByIndex(1)
	sht_2 = ThisComponent.sheets.getByIndex(1)
	if sht_1 is sht_2 then
		MsgBox "True"
	else
		MsgBox "Not True " & sht_1.name & " " & sht_2.name
	end if
end sub
When I run this I get "Not True" followed by the same sheet name twice.

Re: The "is" operator

Posted: Wed May 21, 2025 8:17 pm
by floris v
You should contact the developers about this, they don't visit this forum. They have a mailing list where you can subscribe: https://openoffice.apache.org/mailing-l ... ist-public, then you can ask your question there.

Re: The "is" operator

Posted: Thu May 22, 2025 12:23 am
by Lupp
I never used the "IS operator" in decades and didn't even know that it existed. As far as I can see also Andrew Pitonyak doesn't mention it.
My way to test UNO objects for equality was the (Basic) Function EqualUNOObjects().
However I got the same problem. See my bug report https://bugs.documentfoundation.org/sho ... ?id=125421 .
Unfortunately I didn't understand the attempted answers.

But you find there also a link to an older thread in this forum where @Villeroy points out a solution.
viewtopic.php?f=20&t=98435

Alas! For SheetCell objects and SheetCellRange objects you have the same problem, and the solutions (workarounds using addresses) are very unhandy.

BTW: If you mention statements found in a different community, always give a link, please.

Re: The "is" operator

Posted: Thu May 22, 2025 11:27 am
by karolus
Sheet-NAMES are unique, so why not simply compare

Code: Select all

…
if sht_1.Name = sh_2.Name then
…
end if
…

Re: The "is" operator

Posted: Thu May 22, 2025 8:40 pm
by JeJe
The bug occurs at the sheets level - so will also occur with anything lower down such as an individual sheet.

Its trivial, so long as you're aware of it, as there are easy workarounds.