Jump to content
Laezylion

Function optional parameters : named parameters ?

Recommended Posts

Hello everybody !

I'm using a function that takes one mandatory parameter, and accept several optional parameters.

I'm kinda forced to use ALL parameters when I'll only need one :

Func Example($iOne, $iTwo = 2, $iThree = 3, $iFour = 4)
    ;Whatever ...
EndFunc

Example(10, 10, Default, 40)

Is there any way to call my function like this :

Example(10, ifour = 40)

Because setting all parameters to only use one is kinda boring ... specially with a function that can takes up to 12 different parameters ...

 

I checked the help file with no answers, and digged this forum using this keywords :

"function parameters" named optional

With no results.

 

Thanks in advance for your toughts !

 

Share this post


Link to post
Share on other sites


How about you create a wrapper for the function that you want to use and set all the presets in that function when you call that and it calls down to the auto IT function using those pre-defined parameters


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
31 minutes ago, Earthshine said:


How about you create a wrapper for the function that you want to use and set all the presets in that function when you call that and it calls down to the auto IT function using those pre-defined parameters

Seems to be a lot of work, but in my case, might be worth It ... Will give it a shot !

Share this post


Link to post
Share on other sites

If it doesn't have to be by name you could use something like this to set the parameters by number.
Means: The first parameter goes to element 1 of the parameter array and so on.

Example:

Global $iParamsCount = 12 ; Maximum number of parameters of the function you want to call
Global $aParams[$iParamsCount + 1] = ["CallArgArray"]
; Optional: Set all parameters to "Default"
For $i = 1 To UBound($aParams)-1
    $aParams[$i] = Default
Next
; Set the needed parameters
$aParams[3] = 123
Call(_TestFunction, $aParams)
Exit

Func _TestFunction($vP1 = Default, $vP2 = Default, $vP3 = Default, $vP4 = Default, $vP5 = Default, $vP6 = Default, $vP7 = Default, $vP8 = Default, $vP9 = Default, $vP10 = Default, $vP11 = Default, $vP12 = Default)
    ConsoleWrite("Number of Parameters: " & @NumParams & @CRLF)
    For $i = 1 To @NumParams
        $vParam = Eval("vP" & $i)
        ConsoleWrite("Parameter " & $i & ": " & $vParam & @CRLF)
    Next
EndFunc   ;==>_TestFunction

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...