Sign in to follow this  
Followers 0
Quinch

Passing an arbitrary number of parameters into a function?

7 posts in this topic

Can it be done? For example, if I want to create a function that accept any number of parameters, from three to a thousand, is there a way to do it, well, elegantly?

Share this post


Link to post
Share on other sites



I'd suggest to pass an array as parameter.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

In my OutlookEX UDF I use the following approach:

You can pass up to 10 parameters to a function. If you need more you pack them into an array and pass the array as parameter one.

The code looks like:

; #FUNCTION# ====================================================================================================================
; Name ..........: _OL_DistListMemberAdd
; Description ...: Adds one or multiple members to a distribution list.
; Syntax.........: _OL_DistListMemberAdd($oOL, $vOL_Item, $sOL_StoreID, $vOL_P1 = ""[, $vOL_P2 = ""[, $vOL_P3 = ""[, $vOL_P4 = ""[, $vOL_P5 = ""[, $vOL_P6 = ""[, $vOL_P7 = ""[, $vOL_P8 = ""[, $vOL_P9 = ""[, $vOL_P10 = ""]]]]]]]]])
; Parameters ....: $oOL      - Outlook object returned by a preceding call to _OL_Open()
;                 $vOL_Item - EntryID or object of the distribution list item
;                 $sOL_StoreID - StoreID where the EntryID is stored. Use the keyword "Default" to use the users mailbox
;                 $vOL_P1     - Member to add to the distribution list. Either a recipient object or the recipients name to be resolved
;                 +           or a zero based one-dimensional array with unlimited number of members
;                 $vOL_P2     - Optional: member to add to the distribution list. Either a recipient object or the recipients name to be resolved
;                 $vOL_P3     - Optional: Same as $vOL_P2
;                 $vOL_P4     - Optional: Same as $vOL_P2
;                 $vOL_P5     - Optional: Same as $vOL_P2
;                 $vOL_P6     - Optional: Same as $vOL_P2
;                 $vOL_P7     - Optional: Same as $vOL_P2
;                 $vOL_P8     - Optional: Same as $vOL_P2
;                 $vOL_P9     - Optional: Same as $vOL_P2
;                 $vOL_P10   - Optional: Same as $vOL_P2
; Return values .: Success - Distribution list object
;                 Failure - Returns 0 and sets @error:
;                 |1 - No distribution list item specified
;                 |2 - Item could not be found. EntryID might be wrong
;                 |3 - Error adding member to the distribution list. @extended = number of the invalid member (zero based)
;                 |4 - Member name could not be resolved. @extended = number of the invalid member (zero based)
; Author ........: water
; Modified.......:
; Remarks .......: $vOL_P2 to $vOL_P10 will be ignored if $vOL_P1 is an array of members
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _OL_DistListMemberAdd($oOL, $vOL_Item, $sOL_StoreID, $vOL_P1, $vOL_P2 = "", $vOL_P3 = "", $vOL_P4 = "", $vOL_P5 = "", $vOL_P6 = "", $vOL_P7 = "", $vOL_P8 = "", $vOL_P9 = "", $vOL_P10 = "")

    Local $oOL_Recipient, $aOL_Recipients[10]
    If $vOL_Item = "" Then Return SetError(1, 0, 0)
    If Not IsObj($vOL_Item) Then $vOL_Item = $oOL.Session.GetItemFromID($vOL_Item, $sOL_StoreID)
    If @error <> 0 Then Return SetError(2, @error, 0)
    ; Move members into an array
    If Not IsArray($vOL_P1) Then
        $aOL_Recipients[0] = $vOL_P1
        $aOL_Recipients[1] = $vOL_P2
        $aOL_Recipients[2] = $vOL_P3
        $aOL_Recipients[3] = $vOL_P4
        $aOL_Recipients[4] = $vOL_P5
        $aOL_Recipients[5] = $vOL_P6
        $aOL_Recipients[6] = $vOL_P7
        $aOL_Recipients[7] = $vOL_P8
        $aOL_Recipients[8] = $vOL_P9
        $aOL_Recipients[9] = $vOL_P10
    Else
        $aOL_Recipients = $vOL_P1
    EndIf
    ; Rest of the function

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I'd suggest to pass an array as parameter.

Eh, I spoze it'll have to do. Thanks, though.

Share this post


Link to post
Share on other sites

Why not use CMD parameters?

Note : only 63 parameters can be return by $CmdLine[...], but $CmdLineRaw will always returns the entire command line.

So if they are more then 63 - you can use $CmdLineRaw and split it manually

Share this post


Link to post
Share on other sites

Why not use CMD parameters?

Note : only 63 parameters can be return by $CmdLine[...], but $CmdLineRaw will always returns the entire command line.

So if they are more then 63 - you can use $CmdLineRaw and split it manually

He's been talking about internal functions, right?

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

Some of my allow up to 100 values to be sent to the function. I could have used an array, but chose not to do so. I don't know if there is a limit to the number of parameters you can pass to a function.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0