Jump to content

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 !

 

Link to comment
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

 

Link to comment
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 !

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...