Bug 99554 - With expression re-evaluated for every use within block
Summary: With expression re-evaluated for every use within block
Status: RESOLVED DUPLICATE of bug 132064
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
5.1.2.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-28 14:34 UTC by Mark_T2016
Modified: 2022-03-17 07:02 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark_T2016 2016-04-28 14:34:29 UTC
I noticed LibreOffice Basic evaluates the with expression every time the object is used within the block. This prevents optimizing some code where the object can be evaluated outside of a for/to loop, and then re-used for each iteration.

Workaround is to evaluate the expression to an object and then use that object in the with expression.
For example instead of:
With getsheet
Use:
oSheet = getsheet
With oSheet

This is different behavior compared to Excel VBA, where the object expression is only evaluated at the start of the block. Verified in Excel 2010, almost certain 2007 was the same, but not sure about earlier versions.

Example in LibreOffice Basic:-
Sub Main
	Dim i As Integer
	With getsheet
		For i = 0 To 9
			msgbox (.getCellByPosition(0, i).Value)
		Next i
	End With
End Sub
Function getsheet As Object
	msgbox "getsheet was called"
	getsheet = thisComponent.Sheets(0)
End Function

Similar example in Excel VBA:-
Sub Main()
    Dim i As Integer
    With getsheet
        For i = 1 To 10
            MsgBox (.Cells(i, 1).Value)
        Next i
    End With
End Sub
Function getsheet() As Worksheet
    MsgBox "getsheet was called"
    Set getsheet = ThisWorkbook.Sheets(1)
End Function
Comment 1 Buovjaga 2016-05-06 13:03:05 UTC
Confirmed.

Arch Linux 64-bit, KDE Plasma 5
Version: 5.2.0.0.alpha1+
Build ID: 540fee2dc7553152914f7f1d8a41921e765087ef
CPU Threads: 8; OS Version: Linux 4.5; UI Render: default; 
Locale: fi-FI (fi_FI.UTF-8)
Built on April 30th 2016
Comment 2 Mike Kaganski 2022-03-17 07:02:01 UTC
Let me mark it as a dupe of another one, which, even though filed later, describes the problems with this bug.

*** This bug has been marked as a duplicate of bug 132064 ***