[Solved] Appending to array problem

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
zwora
Posts: 35
Joined: Sun Oct 07, 2018 8:21 am

[Solved] Appending to array problem

Post by zwora »

Hi to Everyone

I am trying to get connection to an external database, but got stuck on appending data to an array. I am using functions from "OpenOffice.org Base Macro Programming By Andrew Pitonyak" which look like below:

Code: Select all

Sub AppendToArray(oData(), ByVal x)
	Dim iUB As Integer 'The upper bound of the array.
	Dim iLB As Integer 'The lower bound of the array.
	iUB = UBound(oData()) + 1
	iLB = LBound(oData())
	ReDim Preserve oData(iLB To iUB)
	oData(iUB) = x
End Sub

Function CreateProperty(sName$, oValue) As com.sun.star.beans.PropertyValue
	Dim oProperty As New com.sun.star.beans.PropertyValue
	oProperty.Name = sName
	oProperty.Value = oValue
	CreateProperty() = oProperty
End Function

Sub AppendProperty(oProperties(), sName As String, ByVal oValue)
	AppendToArray(oProperties(), CreateProperty(sName,oValue))
End Sub
And I am trying to test them using very simple code (below), which gives me a prompt: "Array needs to be dimensioned" (or something similar, because I use not English version of OO, so this is my translation).

Code: Select all

Sub send2DB
	Dim sUserName$
	Dim oParams As New com.sun.star.beans.PropertyValue
	sUserName = "testuser"
	AppendProperty(oParams(), "user", sUserName)
End Sub
Could anyone point me, what is wrong? Thank you.
Last edited by Hagar Delest on Mon Oct 08, 2018 9:24 pm, edited 1 time in total.
Reason: tagged solved
Windows 7/Windows 10, Open Office 4.1.2
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Appending to array problem

Post by JeJe »

Dim oParams() As New com.sun.star.beans.PropertyValue
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zwora
Posts: 35
Joined: Sun Oct 07, 2018 8:21 am

Re: Appending to array problem

Post by zwora »

I tried that before, as some exaples in the internet include () but it seems, that property is not added.

If I add () and then the line on the end: print oParams(0).user it gives the prompt: "Method: user not found"
Windows 7/Windows 10, Open Office 4.1.2
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Appending to array problem

Post by JeJe »

Its oParams(0).name
(= "user")
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Appending to array problem

Post by Villeroy »

When the array variable is instatiated but not having any elements, the upper bound is lower than the lower bound.

Code: Select all

Sub AppendToArray(oData(), ByVal x)
   Dim iUB As Integer 'The upper bound of the array.
   Dim iLB As Integer 'The lower bound of the array.
   iUB = UBound(oData())
   iLB = LBound(oData())
if iUB < iLB then
  Redim oData(iLB)
else
   ReDim Preserve oData(iLB To iUB +1)
endif
   oData(iUB +1) = x
End Sub
Of course you could also use a macro language better than StarBasic. A language which fully supports arrays.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Appending to array problem

Post by JeJe »

If we're tidying things we just need one sub and its best to usually stick with 0 as the LBound, then you don't need to worry about the LBound

Code: Select all

Sub AppendToPropertyArray(oData(),sName As String, ByVal oValue)
	dim IUB as long
	iUB = UBound(oData()) + 1
	ReDim Preserve oData(iUB)
	oData(iUB) =  New com.sun.star.beans.PropertyValue
	oData(iUB).Name = sName
	oData(iUB).Value = oValue
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
zwora
Posts: 35
Joined: Sun Oct 07, 2018 8:21 am

Re: Appending to array problem [Solved]

Post by zwora »

Thank you for your replies. This helped me, and problem I asked is now solved. I've got another problem, but will create new topic, because current topic does not cover the things I would like to ask. Thank you again.
Windows 7/Windows 10, Open Office 4.1.2
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Appending to array problem

Post by Villeroy »

JeJe wrote:If we're tidying things we just need one sub and its best to usually stick with 0 as the LBound, then you don't need to worry about the LBound
I prefer small routines where each one performs one task properly.
One routine to generate property values with one name (string) and one value (any type).
Another routine which appends an arbitrary item to an arbitrary array, no matter which data types, no matter which boundaries.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Appending to array problem

Post by JeJe »

Villeroy: everyone has their own ways of doing things. Adding an item to the end of an array is trivial so I'd possibly have put the code in send2DB and only had one sub.
Villeroy wrote: Of course you could also use a macro language better than StarBasic. A language which fully supports arrays.
I've been working on a library of array functions and classes (sorted array class, associative array class etc) for handling arrays in OOBasic... I'll put it up eventually...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply