How to define a global structure in LOCalc

Hello,
I’m on LO 24.8.6.2 running on MacOS - and it’s Calc Basic Macros.

I have a data structure defined in a user defined library which I wish to access in the standard library for the workbook.
I cannot see how to achieve this. I get Not defined error when I try to define a variable with this type.
I have tried putting “global” in front of the type definition - no change.
The user defined library is defintely loaded (checked with BasicLibraries.isLibraryLoaded )
All functions and subs from this library are working fine in the standard library modules.
All global consts and variables defined in the UDLibrary are also available in the standard library.

So - please could someone advise me on how to achieve a global typedef.

Many thanks,

M.

and/or constructed ? :thinking:

Some module:

REM  *****  BASIC  *****
Public Type Foo
	TXT As String
	NUM As Double
End Type

global foo as Foo

Sub setFoo(s As String, n As Double)
	foo.TXT = s
	foo.NUM = n
End Sub

Function getFoo()
	getFoo = foo
End Function

Some other module:

Sub Main
setFoo "foo", 43
f = getFoo()
print f.TXT, f.NUM
End Sub

Hi Villeroy,
thanks for your reply. That is a very good and clear example of how to use UserDefined type and global variables from that structure. Unfortunately that is not what I was after.
I may not have been quite clear in my question so let me try to explain further.
What I need to do is put a new Type definition in a library and module that is NOT the current library/module. I need then to define variables and functions to be this type in other library/modules.

I’ve uploaded an example spreadsheet to show what I mean.
TestWkBk.ods (11.3 KB)
This has the structure :

Spreadsheet TestWkBk.ods
Contains
    Library Standard
        Module Module1
            Function StdFuncA
        Module CalcRoutines
            Function CalcFuncA
    Library GlbLib
        Module GlbDefn
            Definitions

In GlbLib/GlbDefn
    Type    UDType
        UDVarA as string
        UDVarB as long
        UDVarC as long
        End Type

As you will see in the file in Standard / CalcRoutines / CalcFuncA I type the function as UDType and this does not work. I get the error

image

If I put the type definition into that module, then the error goes away. So module level type definitions are obviously OK.

By using a global Type Definition I am trying to:
a) make the definition available anywhere I need it (just like built-in ones e.g. integer, long …)
b) Ensure consistency when (and it is when) the typedef changes

I look forward to any replies.

regards

M.

Well, the solution is a simple pair of get/set routines.

Swift reply - thanks.

I can see what you are saying and agree it would get round quite a lot of things.
If I understand correctly then the global variable foo is used to work within the other modules, and the function f “autotypes” because of the assignment of foo.

The routines I am working up would sometimes be able to use a global var - other times would definitely only require a function/sub scope variable of the UDType.
I think that would mean needing a “scope Global Var” for each specific usage.
e.g.
If we introduced Function bar into “some other module” and it needed to use the same structure, but with different data than foo (preserving foo) we would have to introduce Global bar as Foo.

Do I have the correct take on this ?

M.

A question spoken aside:
Did you actually work on a regular basisi with user defined types the standard way with type definitions and variable dimensioning using these types in the same library and module?
Years ago when i tried to do so, there showed issues and effects which made me abandon the usage of UDTs completely.
About one of the issues I was in contact with Andrew Pitonyak, and he created the bug report tdf#146082.
I meanwhile forgot about the others.

You can call LibraryA.Module1.getFoo() and you can call LibraryB.Module2.getFoo().
Variables declared as Global “survive” the runtime of your code. After no more Basic code is running, you can access the variable content when you start Basic code the next time.
When it comes to object orientation in LibreOffice macro programming, I resort to Python.

I agree.
This is not ideal - but will work and I think get terribly messy (in my head !!) remembering which library and module and scope etc.
Many thanks for your help.
I think I will probably head over to Py and wallow in some OO’ness.

thanks again.
M.

I forgot to mention that there is some OO magic in StarBasic, but I never tried:
https://help.libreoffice.org/latest/de/text/sbasic/shared/classmodule.html?DbPAR=BASIC#N0082

hi Lupp,
thanks for the “aside” reply.
Yup I do often use UserDef Types as you describe. It can make life simpler and code more readable.
I do prefer a modular approach hence this question arising.

I’ve had a look at your bug - very interesting - I will watch out for any such symptoms.

M

vielen danke.
That could be interesting - maybe have a play later.

cheers
M.

(post deleted by author)