Array as optional parameter

Windows specific questions.
E Dingsor
Posts: 17
Joined: Nov 15, 2017 9:53
Location: Norway

Array as optional parameter

Postby E Dingsor » Nov 05, 2018 10:51

Hi,
I'm trying to make a function with an Optional Array parameter, but compiler throws out Error 59: Illegal specification..
Is it possible to have an optional array parameter?
I've tried these variants, but all give me Error 59
FUNCTION BusinessDays(lStartDate AS LONG, lEndDate AS LONG, lHols() AS long = 0) AS LONG EXPORT
FUNCTION BusinessDays(lStartDate AS LONG, lEndDate AS LONG, lHols() AS long = {0}) AS LONG EXPORT
FUNCTION BusinessDays(lStartDate AS LONG, lEndDate AS LONG, lHols() AS long = ({0})) AS LONG EXPORT
\e
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Array as optional parameter

Postby fxm » Nov 05, 2018 12:27

As the '=' (assignment) operator is not defined for raw arrays (only for object containing arrays), a raw array cannot be passed to a procedure as optional parameter.
Only the '=' (or '=>') can be used for initializing of array at its declaration line level.
MrSwiss
Posts: 2862
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Array as optional parameter

Postby MrSwiss » Nov 05, 2018 12:31

The problem with the array is the 'optional', in this context.
I'm just going to ignore 'export' here (supposedly a DLL's code, but currently in a first
step, not of consequence ...)

lHols() AS long = 0 ' you are trying to assign (0) to a array without an index (throws error)

What you really want to do is a comparison (not an assignment).
A workaround is to use an array ptr, instead of the array (itself) as a parameter, to be
able to check its validity.

Ptr access to array:

Code: Select all

Declare FUNCTION BusinessDays( ByVal lStartDate AS LONG, ByVal lEndDate AS LONG, ByVal pa As Long Ptr = 0 ) AS Long

Dim As Long a(Any)                      ' dynamic array
ReDim a(0 To 99)                        ' initialize array
Dim As Long Ptr pa = @a(0)              ' array pointer

Dim As Long result = BusinessDays(100, 101)
Print "array ptr not given: "; Str(result)

result = BusinessDays(100, 101, pa)
Print "array ptr existing: "; Str(result)
Sleep

FUNCTION BusinessDays( _
    ByVal lStartDate AS LONG, _
    ByVal lEndDate AS LONG, _
    ByVal pa As Long Ptr = 0 _
    ) AS Long
    Dim As Long temp = 777
    ' above: just for testing purposes
    If pa = 0 Then
        temp = 0
    Else
        ' ...
    End If
    Return temp
End Function
fxm
Posts: 8516
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: Array as optional parameter

Postby fxm » Nov 05, 2018 12:53

The better (IMHO) is to encapsulate the array in a Type:

Code: Select all

Type UDT
  Dim As Long array(Any)
End Type

Dim Shared As UDT u0
Redim u0.array(3 To 5)
u0.array(4) = 789

Sub test (Byref u As UDT = u0)
  Print Lbound(u.array), Ubound(u.array), u.array(4)
  u.array(3) = 456
End Sub

test()
Print u0.array(3), u0.array(4)

Sleep
E Dingsor
Posts: 17
Joined: Nov 15, 2017 9:53
Location: Norway

Re: Array as optional parameter

Postby E Dingsor » Nov 05, 2018 13:20

Hi,
Thanks for valuable suggestions. I'm porting some code from PowerBasic and hoped to avoid going down the pointer route, but it seems that is the way to go.
\e
E Dingsor
Posts: 17
Joined: Nov 15, 2017 9:53
Location: Norway

Solved: Array as optional parameter

Postby E Dingsor » Nov 13, 2018 6:44

Made the "optional" route work with passing an array and checking upper bound like this.

Function BusinessdaysBetween(lStartdate as long, lEndDate as long, lHolidays() as long) as long
dim bHoliday as Boolean
'if array with holidays is passed "empty" the lHols() array will be not dimensioned i.e
IF UBOUND(lHols) = [b]- 1
then bHoliday = FALSE ' don't try to access the lHols array. It contains nothing!
else
bHoliday = TRUE
END IF
.....
.....
End function

note. lHols() is defined like this initially:
dim lHols() as long. If list of holidays is past elsewhere(in code) the array will be re-dimensioned and filled with holiday dates.
Maybe not the fanciest solution, but it works!
\E

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 2 guests