Bug 151005 - With "Option VBASupport 1" arguments should be treated as ByValue by default (not as ByRef)
Summary: With "Option VBASupport 1" arguments should be treated as ByValue by default ...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
7.3.5.2 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Macro-VBA
  Show dependency treegraph
 
Reported: 2022-09-16 18:29 UTC by Rafael Lima
Modified: 2023-03-03 15:27 UTC (History)
2 users (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 Rafael Lima 2022-09-16 18:29:44 UTC
This ticket is a spinoff from bug 150727.

LibreOffice's Basic implementation assumes that arguments are defined as ByRef (see [1]). So in LO the code below prints "Y" as a result, which is correct.

Sub Test()
    Dim a As String
    a = "X"
    DummySub (a)
    MsgBox (a)
End Sub

Sub DummySub(val As String)
    val = "Y"
End Sub

[1] https://help.libreoffice.org/7.4/en-US/text/sbasic/shared/03090409.html

However, in MS VBA, arguments are passed as ByValue by default, hence the code above using Option VBASupport 1 should print "X" in LibreOffice, but we still get "Y" as a result.

If you run the code above in MSO you'll get an "X" as result.
Comment 1 tanh 2022-11-20 02:18:53 UTC
In MS VBA, arguments are passed BYREF as default. (Documentation claims that VB.NET is different).

In MS VBA the test case shown creates a temporary reference by using an expression

DummySub (a)

The expression (a) is created, referenced, updated to 'Y', and discarded.

Subsequent use of the variable a is not affected by the discarded (expression)


To demonstrate the same behavior using Call syntax:

Call DummySub( (a) )

Wrapping a variable in () creates an expression. The CALL syntax requires call brackets, which are not expression brackets.
Comment 2 tanh 2022-11-20 02:35:16 UTC
In LO, the statement
DummySub (a)

Does NOT create and discard a reference. Rather, it acts the same way as the CALL syntax


LibreOffice: DummySub(a) is the same as Call DummySub(a).
MS VBA: DummySub (a) is NOT the same as Call DummySub(a)

LibreOffice: DummySub2(a,b) is NOT a syntax error.
MS VBA: DummySub2(a,b) IS a syntax error.
MS VBA: Call DummySub2(a,b) is NOT a syntax error.

LibreOffice: Call DummySub2( (a), b) is the same as Call DummySub2( a,b)
MS VBA: Call DummySub2( (a), b) is NOT the same as Call DummySub2( a,b)

==========
sub Test()
    Dim a As String,b
    a = "X"
    call DummySub2((a),b)
    MsgBox (a)
End Sub

Sub DummySub2(val As String,b)
    val = "Y"
End Sub
==========