[Solved] Passing array's by reference

Creating a macro - Writing a Script - Using the API

[Solved] Passing array's by reference

Postby janfl » Mon Oct 02, 2017 9:20 am

Hi all,

I have two examples. The first passes a array "@tbstps" by reference:
Code: Select all   Expand viewCollapse view
COMARRAY(tbstps,10)
oStyle.SetPropertyValue("ParaTabStops",@tbstps) &&With XRay I can see that the property has been set well


The second passes the array "tbstps"by value:
Code: Select all   Expand viewCollapse view
COMARRAY(tbstps,0)
*oStyle.ParatabStops=tbstps &&With Xray I see that the property has not been changed.


Can I conclude that the API/Com does only handle array's by reference?

Kind regards,


Jan Flikweert
Last edited by janfl on Fri Oct 06, 2017 9:18 pm, edited 2 times in total.
Open Office 4.1.3 Windows 10
janfl
 
Posts: 29
Joined: Thu Sep 28, 2017 4:17 pm

Re: Passing array's by reference

Postby janfl » Thu Oct 05, 2017 2:16 pm

I think the next way

Code: Select all   Expand viewCollapse view
Public args(1)
args(1) = Createobject('Empty')
=ADDPROPERTY(args(1),"Name")
=ADDPROPERTY(args(1),"Value")
args(1).Name = "Tabstops"
*args(1).Value = Array(Array(499,"com.sun.star.style.TabAlign.LEFT",","," "),Array(6249,"com.sun.star.style.TabAlign.LEFT",","," "),Array(6750,"com.sun.star.style.TabAlign.LEFT",","," "))
args(1).Value = 500
Tabstops.setpropertyvalue(args1().name,args1().value)


Could be mentioned by reference?

On the VFP Forum AtoutFox.org http://atoutfox.org/nntp.asp?ID=0000018481 there is also a nice solution.

Remains the question how to adress the next array of an array: Array(Array(499,"com.sun.star.style.TabAlign.LEFT",","," "),Array(6249,"com.sun.star.style.TabAlign.LEFT",","," from VFP to OO????????

Kind regards,


Jan Florijn
Open Office 4.1.3 Windows 10
janfl
 
Posts: 29
Joined: Thu Sep 28, 2017 4:17 pm

Re: Passing array's by reference

Postby Lupp » Thu Oct 05, 2017 3:50 pm

The kind of code fragments is hard to interpret. What laguage/IDE actually? What options set? What extensions available?

Generally I can only state that you cannot by "=" assign an object / a structure to a respective property. For objects the "=" doesn't move data, but creates an access. You either are using a variable that acts as a placeholder for the original object. No additional assignments needed after that variable got assigned values to the elements. Or you have a copy of the structure including the data outside of the object. In this case you need to use the appropriate set-method.
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2489
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Passing array's by reference

Postby janfl » Thu Oct 05, 2017 4:06 pm

Lupp wrote:The kind of code fragments is hard to interpret. What laguage/IDE actually? What options set? What extensions available?

Generally I can only state that you cannot by "=" assign an object / a structure to a respective property. For objects the "=" doesn't move data, but creates an access. You either are using a variable that acts as a placeholder for the original object. No additional assignments needed after that variable got assigned values to the elements. Or you have a copy of the structure including the data outside of the object. In this case you need to use the appropriate set-method.


Hi Lupp,

Thanks for your reply.

I work with Visual Fox Pro 9.0 sp2.

This example is regarding OpenOffice not quite correct. It is a idea how to pass variables by name. As you state "='gives a placeholder. That is exact the meaning.

In the last line I use setpropertyvalue and place there a reference by name. See is as loud thinking.

OpenOffice passes variables by name and not by value.

VFP does pass arrays by value.

So this is a VFP related problem.

Kind regards,


Jan Flikweert
Open Office 4.1.3 Windows 10
janfl
 
Posts: 29
Joined: Thu Sep 28, 2017 4:17 pm

Re: Passing array's by reference

Postby Lupp » Thu Oct 05, 2017 5:38 pm

janfl wrote:OpenOffice passes variables by name and not by value.

That's not quite correct. First I want to make clear that I understand "by name" here as "by reference". The same reference can be assigned to more than one name in relevant cases as seems to be common to object oriented programming or even in a wider range. AOO BASIC gives you the choice how a Sub or a Function treats the passed parameter.

Default is "by reference", but you can use the 'ByVal' clause prefixed to a formal parameter to change that. If you do, the routine (Sub or Function) will create a local variable accessed with the name of the formal parameter and containing the de-referenced data. In case of a call from a formula in Calc, e.g, this is (mostly) already done by the evaluator.

However, if already the actual parameter passed when calling the routine is a name or an expression for a refernce ('Pointer'), the corresponding formal 'ByVal' parameter will, though created locally, again only contain the pointer (reference), not the data. In fact the routine (or the parts of the system helping it run) would need to know and to use a lot of additional information to be able to de-reference the data. I cannot imagine a system that actually does so, except in very specialized cases where the structure of the passed "thing" is either known to the routine anyway, or allows for a very simple description that can be passed in addition, as may be the case for arrays of simple scalar values. Standard seems to be to pass "dynamic arrays" as just 1D data + 'High' info, and leave the rest to the routine.

(Not being a programming expert, I hope nonetheless this makes sense.)
On Windows 10: LibreOffice 6.2 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2489
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests