Missing parameter problem & its evaluation
Missing parameter problem & its evaluation
Please tell me how to use missing parameters in StarBasic correctly.
My code is using option VBASupport:
Option VBASupport 1
1. Function UDF(Optional bStatus As Boolean = True)
The following statement works without failures in VBA, but here I had to drop it.
<Optional bStatus As Boolean = True> works unstable, crashes (of course, in my context)
2. Function UDF(Optional bStatus As Boolean)
If IsMissing(bStatus) Then: End If
And bStatus will automatically be set to False after calling the IsMissing function. Why?
If IsMissing(bStatus) Then bStatus = True
Alas, the missing parameter after the call to the IsMissing function is set to False and is no longer considered missing and will not be assigned a new value of True. Is this a bug?
3. Function UDF(Optional bStatus)
If IsMissing(bStatus) Then bStatus = True
This is how it works. Variant/Boolean type. But then the "b" prefix is not entirely correct.
My code is using option VBASupport:
Option VBASupport 1
1. Function UDF(Optional bStatus As Boolean = True)
The following statement works without failures in VBA, but here I had to drop it.
<Optional bStatus As Boolean = True> works unstable, crashes (of course, in my context)
2. Function UDF(Optional bStatus As Boolean)
If IsMissing(bStatus) Then: End If
And bStatus will automatically be set to False after calling the IsMissing function. Why?
If IsMissing(bStatus) Then bStatus = True
Alas, the missing parameter after the call to the IsMissing function is set to False and is no longer considered missing and will not be assigned a new value of True. Is this a bug?
3. Function UDF(Optional bStatus)
If IsMissing(bStatus) Then bStatus = True
This is how it works. Variant/Boolean type. But then the "b" prefix is not entirely correct.
Last edited by eeigor on Sat Apr 24, 2021 12:19 pm, edited 1 time in total.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem
Try to use two variables to handle the optional parameter.
The conventional "b" prefix never will determine the type of the variable. It is only a helper mark for the programmer.
Code: Select all
Function UDF(Optional bPassedStatus As Boolean)
dim bInnerStatus as boolean
If IsMissing(bPassedStatus) then bInnerStatus = True
REM Use the bInnerStatus in the next parts of the function...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Missing parameter problem
Thanks.
Yes, I've seen this practice. This was not the case in VBA. However, I'm not entirely sure if it really should have been done exactly as you advise. It just leads to an increase in the variables. My UDF has 7 parameters, of which only 2 are required.
Just in case, I will ask if there are other opinions?
UPD
Why doesn't the code below work?
If you try to assign a string to an optional parameter of type Variant, an error will occur. The reason is that the parameter will initially be evaluated as an Object because there is a call to the object method .CellRange in the code. Is this a bug?
E.g. Pseudocode
Call a UDF from a sheet cell by passing a cell or range reference and the name (address) should be output.
Call the TestIt procedure and there will be a parameter type error.
In VBA, there is no such connection between an input parameter and a procedure code. And moreover, with code that will not be executed in a particular case. This is where the second variable is really needed, but of the Variant type. Some kind of nonsense...
Yes, I've seen this practice. This was not the case in VBA. However, I'm not entirely sure if it really should have been done exactly as you advise. It just leads to an increase in the variables. My UDF has 7 parameters, of which only 2 are required.
Just in case, I will ask if there are other opinions?
UPD
Why doesn't the code below work?
If you try to assign a string to an optional parameter of type Variant, an error will occur. The reason is that the parameter will initially be evaluated as an Object because there is a call to the object method .CellRange in the code. Is this a bug?
E.g. Pseudocode
Call a UDF from a sheet cell by passing a cell or range reference and the name (address) should be output.
Call the TestIt procedure and there will be a parameter type error.
Code: Select all
Option VBASupport 1
Sub TestIt()
Call UDF("TestIt")
End Sub
Function UDF(Optional Caller)
Dim sCallerName$
If Not IsMissing(Caller) Then
If TypeName(Caller) = "Range" Then 'Excel VBA
' Retrieve the fully qualified reference to the Caller.
sCallerName = Caller.CellRange.AbsoluteName
ElseIf TypeName(Caller) = "String" Then
sCallerName = Caller
End If
MsgBox sCallerName
End If
End Function
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
It is not a good idea to use the VBA procedures in the LibreOffice, in the Apache OpenOffice. Use the API functions and procedures. The MS VBA and the AOO/LO never will be fully compatible.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Missing parameter problem & its evaluation
No, no, I only use VBA for the sole purpose of passing a range reference to the UDF, but the question was different. Understand how variables are handled.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
Same solution to avoid an error.
In Basic you can't set a variable to a value on declaration so you can't put "optional varname =" or "varname ="
Edit: and you'll need to check to see if "Range" is implemented as a typename.
Code: Select all
Option VBASupport 1
Sub TestIt()
Call UDF("TestIt")
End Sub
Function UDF(Optional Caller)
Dim sCallerName$,t
If Not IsMissing(Caller) Then
If TypeName(Caller) = "Range" Then 'Excel VBA
' Retrieve the fully qualified reference to the Caller.
t = caller
sCallerName =t.CellRange.AbsoluteName
ElseIf TypeName(Caller) = "String" Then
sCallerName = Caller
End If
MsgBox sCallerName
End If
End Function
Edit: and you'll need to check to see if "Range" is implemented as a typename.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Missing parameter problem & its evaluation
@JeJe, thank you. Now a complete understanding has been reached. You enter a variable t of type Variant. What is the reason for this approach? In VBA, everything is simpler and, if you want, cleaner. Give your opinion on the reasons for all that I have touched upon here. Is StarBasic not finalized yet?
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
I'm sorry, I didn't read your post to the endJeJe wrote:In Basic you can't set a variable to a value on declaration so you can't put "optional varname =" or "varname ="
Really, I can, if VBA Support mode is enabled.
Function UDF(Optional bStatus As Boolean = True)
This is not the first time I have encountered this feature. For example, why is the obj variable introduced in the code below? Try this: oDoc.SupportsService(), and get an error.
Code: Select all
Sub Test_IsCalcDocument()
Print IsCalcDocument("Hello"); Chr$(9); IsCalcDocument(ThisComponent)
End Sub
Function IsCalcDocument(oDoc) As Boolean
On Error GoTo Failed
Dim obj: obj = oDoc 'avoids the error
' IsCalcDocument = False
If obj.SupportsService("com.sun.star.sheet.SpreadsheetDocument") Then
IsCalcDocument = True
End If
Failed:
End Function
In short, I don't understand something here:
Dim obj: obj = oDoc 'avoids the error
- Attachments
-
- Снимок экрана от 2021-04-24 17-07-55.png (9.23 KiB) Viewed 16025 times
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
You're right! Didn't know that... that could be useful!Really, I can, if VBA Support mode is enabled.
Function UDF(Optional bStatus As Boolean = True)
*
Wild guess... maybe the compiler checks through the sub/function to make sure any variables there and in the sub declaration match for type?
I think you just have to accept how OOBasic does things and work with it... Office API aside its almost a clone of the VB family of languages. There are quirks like with the split function:
viewtopic.php?f=20&t=95717
You just have to live with them and work round it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Missing parameter problem & its evaluation
Yes, of course. It's just that I'm still at the stage of mastering these compiler features. In the Pitonyak manual, these details are not reflected in the details. Although I tend to consider these features a disadvantage.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
To be honest: I only had a fly over this thread. Too much VBA.
However, it might be releated somehow to this bug: https://bugs.documentfoundation.org/sho ... ?id=102381.
Concerning optional parameters which actually were omitted in the call, the behaviour gravely depends on whether the call came from the Calc evaluator or from a different Basic routine. Calc wrongly passes 0 (Double) for a missing optional parameter to a user function in Basic.
(In addition there is a fundamental problem with trying to omit a parameter when calling a standard function via the FunctionAccess service. The implications of omitting parameters are "a bit" messed up.)
However, it might be releated somehow to this bug: https://bugs.documentfoundation.org/sho ... ?id=102381.
Concerning optional parameters which actually were omitted in the call, the behaviour gravely depends on whether the call came from the Calc evaluator or from a different Basic routine. Calc wrongly passes 0 (Double) for a missing optional parameter to a user function in Basic.
(In addition there is a fundamental problem with trying to omit a parameter when calling a standard function via the FunctionAccess service. The implications of omitting parameters are "a bit" messed up.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München
Re: Missing parameter problem & its evaluation
You can dump layers and layers and more layers on this API until you end up with dependency hell, security issues, performance issues in very poor and unmaintained amateur code. Look at website development and all the layers of silly JavaScript libraries for the stupid.
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Missing parameter problem & its evaluation
@Lupp thanks for the valuable observations. And the bug you mentioned is still present.
My procedure just assumes two calls: from the sheet and from the code.
I plan to take the time and test everything as it should. I do not have complete clarity today. This makes the UDF look unfinished.
Everything works if you declare the parameters as Variant and assign the passed values to internal variables. But I want to get rid of the redundancy of the code.
My procedure just assumes two calls: from the sheet and from the code.
I plan to take the time and test everything as it should. I do not have complete clarity today. This makes the UDF look unfinished.
Everything works if you declare the parameters as Variant and assign the passed values to internal variables. But I want to get rid of the redundancy of the code.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
Compare two calls.
Run PassParamsByType func from a sheet cell (screenshot 1), and then run Test_PassParamsByTypeFromModule (screenshot 2). Complete confusion... And @Lupp was right.
A1: =PASSPARAMSBYTYPE(1;;;;;)
Pay attention to the boolean parameter handling and look at my remarks in the start post. And the string parameter is set to zero.
Screenshots 1 & 2:
Run PassParamsByType func from a sheet cell (screenshot 1), and then run Test_PassParamsByTypeFromModule (screenshot 2). Complete confusion... And @Lupp was right.
A1: =PASSPARAMSBYTYPE(1;;;;;)
Code: Select all
Function PassParamsByType(pRequired _
, Optional pVariant, Optional pLong&, Optional pDouble#, Optional pString$, Optional pBoolean As Boolean)
If IsMissing(pBoolean) Then
pBoolean = True
End If
MsgBox "Parameters Passed" _
& Chr(10) & "TypeName(pRequired):=" & TypeName(pRequired) _
& Chr(10) & "pVariant:=" & pVariant _
& Chr(10) & "pLong:=" & pLong _
& Chr(10) & "pDouble:=" & pDouble _
& Chr(10) & "pString:=" & pString _
& Chr(10) & "pBoolean:=" & pBoolean _
, , "macro:PassParamsByType"
End Function
Sub Test_PassParamsByTypeFromModule()
Call PassParamsByType(1 _
, , , , ,)
End Sub
It seems that yes.Zizi64 wrote:Try to use two variables to handle the optional parameter.
Screenshots 1 & 2:
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
When you call the function in this way from the cell
then the parameters are NOT missing, but they have empty (zero) values.
Try it from the cell:
In this case the params are missing really.
You can check irf a parameter is missing or not - really:
I checked it WITHOUT the VBA compatibility option!!!!%!!!!!
Code: Select all
= PASSPARAMSBYTYPE(1;;;;;)
Try it from the cell:
Code: Select all
=PASSPARAMSBYTYPE(1)
Code: Select all
In this case the params are missing really.
You can check irf a parameter is missing or not - really:
Code: Select all
REM ***** BASIC *****
Function PassParamsByType(pRequired, Optional pVariant, Optional pLong&, Optional pDouble#, Optional pString$, Optional pBoolean As Boolean)
If IsMissing(pBoolean) Then
pBoolean = True
print "is missing" REM ------------------------- inserted by Zizi64
End If
MsgBox "Parameters Passed" _
& Chr(10) & "TypeName(pRequired):=" & TypeName(pRequired) _
& Chr(10) & "pVariant:=" & pVariant _
& Chr(10) & "pLong:=" & pLong _
& Chr(10) & "pDouble:=" & pDouble _
& Chr(10) & "pString:=" & pString _
& Chr(10) & "pBoolean:=" & pBoolean _
, , "macro:PassParamsByType"
End Function
Sub Test_PassParamsByTypeFromModule()
Call PassParamsByType(1, , , , ,)
End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Missing parameter problem & its evaluation
Yes, they have. But aren’t the params missing? That is the question.Zizi64 wrote:…then the parameters are NOT missing, but they have empty (zero) values.
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
Re: Missing parameter problem & its evaluation
It seems there is such logic in the Calc: If you marked the place of the passed variables (by the semicolons), then they will be recognized as existing but empty passed parameters in a formula.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Missing parameter problem & its evaluation
We'll have to give up. Such bugs cannot live so long... This is a feature
Ubuntu 18.04 LTS • LibreOffice 7.5.3.2 Community
-
- Posts: 12
- Joined: Mon Oct 30, 2017 12:39 pm
Re: Missing parameter problem & its evaluation
FTR:
1. Basic behavior WRT Optional arguments is explained in https://git.libreoffice.org/core/+/8e32 ... 6b9bf20f78:
2. Calc behavior WRT optional arguments is described in comments to many Calc functions, e.g., STYLE:
If it does, then it is a bug, and should be filed and fixed.
4.
1. Basic behavior WRT Optional arguments is explained in https://git.libreoffice.org/core/+/8e32 ... 6b9bf20f78:
Hence, this quote:In LO Basic, optional parameters are allowed, but without
any default values. Missing parameters will not be initialized
to their respective default values of its datatype, either.
With option Compatible, optional parameters are allowed
with default values. Missing optional parameters that
don't have explicit default values will not be initialized
to their default values of its datatype.
With option VBASupport, optional parameters are allowed with
default values. Missing optional parameters that don't have
explicit default values will be initialized to their default
values of its datatype.
should be either some mistake, or a bug in an (unspecified) version. It works as expected e.g. in my current 7.2.0.2.eeigor wrote:2. Function UDF(Optional bStatus As Boolean)
If IsMissing(bStatus) Then: End If
And bStatus will automatically be set to False after calling the IsMissing function. Why?
If IsMissing(bStatus) Then bStatus = True
Alas, the missing parameter after the call to the IsMissing function is set to False and is no longer considered missing and will not be assigned a new value of True. Is this a bug?
2. Calc behavior WRT optional arguments is described in comments to many Calc functions, e.g., STYLE:
3. Wrt this quote:In the LibreOffice Calc functions, parameters marked as "optional" can be left out only when no parameter follows. For example, in a function with four parameters, where the last two parameters are marked as "optional", you can leave out parameter 4 or parameters 3 and 4, but you cannot leave out parameter 3 alone.
It is completely unclear how this should even behave differently compared toZizi64 wrote:Try to use two variables to handle the optional parameter.
Code: Select all
Function UDF(Optional bPassedStatus As Boolean) dim bInnerStatus as boolean If IsMissing(bPassedStatus) then bInnerStatus = True REM Use the bInnerStatus in the next parts of the function...
Code: Select all
Sub Foo(Optional Bar As Boolean)
If IsMissing(Bar) Then Bar = True
End Sub
4.
See DefBool statement.Zizi64 wrote:The conventional "b" prefix never will determine the type of the variable. It is only a helper mark for the programmer.
LibreOffice 7.6 on Windows 10
Re: Missing parameter problem & its evaluation
It is impossible to give a value to a variable what is not exist.Code: Select all
If IsMissing(Bar) Then Bar = True
Use a local variable for the boolean value with a locally defined default value for that case when the passed variable is not exist.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Missing parameter problem & its evaluation
No, it works like this in VB, VBA and StarBasic since the 90ies of the past millenium. The variable is declared as optional. It is not initialized when the calling routine does not pass it over. Its vartype is 10, isMissing(bar)=True and you can assign a value to it.Zizi64 wrote:It is impossible to give a value to a variable what is not exist.
Stupid Basic where a variable can be empty, missing, null, nothing in addition to the regular types:
Code: Select all
Sub foo(optional bar)
if isMissing(bar) then bar = true
Code: Select all
def foo(bar=True):
If you call foo(False), the value of bar will be False because you override the default value.
Edit: changed "vartype is 9" to "vartype is 10". Thank you JeJe |
Last edited by Villeroy on Thu Aug 05, 2021 12:13 pm, edited 1 time in total.
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Missing parameter problem & its evaluation
Optional parameters are of the Error value type (10) so we could also put:
Code: Select all
Sub foo(optional bar)
if vartype(bar) =10 then bar = true
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
-
- Posts: 12
- Joined: Mon Oct 30, 2017 12:39 pm
Re: Missing parameter problem & its evaluation
Note that errors are not limited to "missing", and there may be valid cases of passing errors as arguments; so IsMissing is more specific and correct check. Also using an error to pass "missing" argument is an undocumented implementation detail, which might change at some point (e.g., it prevents from passing that error as value to a function taking optional "error" argument - so it might be useful to introduce some explicit mechanism of marking missing argument value internally) - so use of VarType for that is conceptually unreliable/not future-proof. (Although of course, if we introduce that explicit internal "missing" flag, we likely would still use same error value, just with that flag set, to minimize breakage of existing code.)JeJe wrote:Optional parameters are of the Error value type (10) so we could also put:
Code: Select all
Sub foo(optional bar) if vartype(bar) =10 then bar = true end sub
LibreOffice 7.6 on Windows 10
Re: Missing parameter problem & its evaluation
Villeroy wrote:Stupid Basic where a variable can be empty, missing, null, nothing in addition to the regular types:
Code: Select all
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
def foo(s):
print(type(s))
foo(12)
foo("foo")
foo(None)
# foo() obviously an error
missing = object()
def foo(s = missing):
print(type(s))
foo(12)
foo("foo")
foo(None)
foo()
print(type(foo))
foo = 12
print(type(foo))
Your comparing apples to oranges here:
Code: Select all
def foo(bar=True):
If you give your basic function parameter a default value it will work exactly like the python parameter.
Python doesn't have optional parameters. You either have a forced parameter or a defaulted one.
LibreOffice 7.1.6.2 on Windows 8