[solved] error access to object array like of PropertyValue inside function

greetings to all. I was trying to build a generalized function that would allow:

a) to create a new like array object of com.sun.star.beans.PropertyValue with pair name-value contained in array a

b) to receive an already existing like array of PropertyValue as input for selective editing of only some elements

the array a in the first case contained only pairs of name-value, in the second triples of name-value-index
Oddly, once created and returned, the PropertyValue array is accessible without error by the calling program
on the contrary, fail after passed the newly created array to the function to modify only one item. Message displayed is "subroutine or function

why?

Thanks in advance for any help (libreoffice: 6.2.8.2 SO:Ubuntu 16.04

sub testPropValueSet()
   dim a,b,x, o , o2
   a=array(array("one", 10), array("two",20), array("three",30))
   o=propValueSet(a)
   msgbox (o(0).name)     ' HERE ACCESS CORRECT, DISPLAYED -one-, NO ERROR 
   b=array(array("",35,2))
   o1=propValueSet(b,o)
  
end sub

function propValueSet(a, optional propVal)   ' ex searchProperties
  dim n, bNew,i,d,r
 
    n=ubound(a)
    bNew=ismissing(propVal)  ' if propVal ismissing, create propVal
    if bNew then 
       dim propVal(n) as new com.sun.star.beans.PropertyValue
    end if
    
    for r= 0 to n
       if bNew then 
          i=r: propVal(i).name=a(i)(0) :propVal(i).value=a(r)(1)  
       else
          i=a(r)(2): dim o as new com.sun.star.beans.PropertyValue
          if a(r)(0) <> "" then 
              o.name=a(r)(0) 
          else 
                                           '  HERE THE ERROR -SUB OR FUNCTION NOT DEFINED-
              d=propVal(i) 
              o.name=d.name : o.value=a(r)(1)
          end if
          
          propVal(i)=o
       end if
 
    next  r
    propValueSet=propVal  
end function

In the meantime, very thanks for your reply and the time you have dedicated to me
Your solution works, does not give error

I need some time to fully understand how it differs from mine.

The difference between the two solutions seems to be your use of a variant array, each item obtained creating a new property value.

I still do not understand, however, for this I need time, why in my solution in the calling code I access without problems to the items of the propVal returned

Contrary, when send back without changes propVal to the same function that created it gives me an error even for a simple access (from debug I verified that propVal it is not changed)

I specify that the structure created and / or modified must then be used as the final parameter in dispatcher.executeDispatch (…, propVal)

Some lines caused bewilderment not only for me, but also for Basic (the Compile button, the first in the panel) - “how can you try to determine propVal through Dim, if it is already described in the function parameter?” asked IDE. I had to carefully read the entire code to understand what exactly was intended. propValueSet needs to be further equipped with input parameter checks. This function could find the required parameter by name, not by index, and change its value. It would be more logical. And the need for the third parameter (index) would disappear.

I realize that the compiler may have problems, but I don’t understand why the internal debug recognizes in the same way propVal both in the calling code and in the function of my code

propVal variable, everywhere in my code, is correctly seen in debug as object (0 to 2) with 3 items each of type propertyValue. Why compiler do not recognizes in the 2° call function ?

I finally understood the meaning of your solution. In the function you never read propVal, and therefore you avoid the read error of my code.

All array of propertyValue examples have a format like:

dim a (n) as new new com.sun.star.beans.PropertyValue
a (0) .name = …
a (0) .value = …
a (n-1) .name =
a (n-1) .value =

Debug Ide display a() as object(0 to 2) of propertyValue and your solution as variant (0 to 2) of propertyValue

I finally tested the dispatcher.executeDispatch (…, a ()) using your variant array and work fine.

Then the practical problem is solved using your solution.

Very thanks

Apart from the final result, however, if a code does not work I am interested in understanding why, to improve my understanding of the star basic formalism

I still have to understand specifically why my solution didn’t work, certainly something in the declaration and in the passing of parameters to the function

on this I have to work on it

Unfortunately, the format of this resource does not provide for a detailed discussion, this is not a forum. It’s a pity. Your little code is a very good sample for dissection - it is not as boring as the usual “Hello world!” and very difficult to get lost in. Having disassembled this sample, you can understand a lot in Basic.For example, why one line ubound(a) in the function is not enough, but you will definitely have to use ubound (propVal)

I’ll work on it. I realize from this particular case that I still miss fundamental concepts on the passing of parameters and the declaration-use of object and array

And I already know that this will give me other problems in the future that cannot be solved with alternative solutions as in this case

Thanks again for the solution and suggestions

Do you know a forum on LO of the same importance as this one, where it’s possible also to discuss on these issues?

I propose to start from two directions at once - from the book of Pitonyak (selective reading, just find in the TOC those fragments that may be relevant to the problem being solved at the moment, for example, chapter 16.2.8. Arrays and 3.6.3. Structures) and read this forum and then write in this forum. Many of us who live here are almost constantly there. But before you start posting on the forum, be sure to look for old posts on topics that interest you. They are very good professionals, but they get nervous when they have to answer the same question for the fifteenth time.

I have a lot of programming experience on other languages, mostly javascript (last 10 years) and VB-VBA (1996-2001)

But I started with LO (application and starbasic) only from June 2019, I have a lot to study

I am studying the two books by pitonyak, the one you recommended to me and the other, AndrewMacro.odt, along with what I find around and here in ask.libreoffice, where over time I should make more continuous contributions.

I know and have consulted the OO forum many times, you are right, it is the case that I also subscribe there

Thanks again for your availability on this and previous occasions

It seems to me that you tried to write something like this:

Option Explicit

Sub testPropValueSet()
Dim a As Variant, b As Variant, x As Variant, o As Variant, o1 As Variant
Dim sMessage As String, i As Integer 
	a=Array(Array("one", 10), Array("two",20), Array("three",30))
	o=propValueSet(a)
	sMessage = ""
	For i = 0 To UBound(o)
		sMessage = sMessage + o(i).Name + "=" + o(i).Value + Chr(10)
	Next i
	MsgBox (sMessage, 0, "First result")
	
	b=array(array("",35,2))
	o1=propValueSet(b,o)
	sMessage = ""
	For i = 0 To UBound(o1)
		sMessage = sMessage + o1(i).Name + "=" + o1(i).Value + Chr(10)
	Next i
	MsgBox (sMessage, 0, "Second result")
End Sub 

Function propValueSet(a As Variant, Optional propVal As Variant) As Variant
Dim n As Integer, i As Integer, r As Integer
Dim bNew As Boolean
Dim d As Variant, o As Variant 
Dim newPropVal()  As Variant 
	n = UBound(a)
	bNew = IsMissing(propVal)  ' if propVal ismissing, create propVal '
	If  bNew Then 
		ReDim newPropVal(n)
		For i = 0 To n
			 newPropVal(i) = New com.sun.star.beans.PropertyValue
		Next i
		propVal = newPropVal
	EndIf 

	n = UBound(propVal)

	For r= 0 to UBound(a)
		If bNew Then	
			propVal(r).name=a(r)(0) 
			propVal(r).value=a(r)(1)  
		Else
			If UBound(a(r)) > 1 Then 
				i=a(r)(2)
			Else 
				MsgBox("Element a(" + r + ") does not contain Index", MB_ICONSTOP, "Wrong parameter")
				Stop 
			EndIf 
				
			If i>n Then 
				MsgBox("Index a(" + r + ")=" + i +  " points to nonexistent element", MB_ICONSTOP, "Wrong parameter")
				Stop 
			EndIf 
			
			o = New com.sun.star.beans.PropertyValue
			o.name=Trim(a(r)(0))
			If o.name = "" Then  
				o.name=propVal(i).Name
			EndIf 
			o.value=a(r)(1)
			propVal(i)=o
		EndIf 
	Next r
	propValueSet=propVal  
End Function

I believe in you :wink: