Jump to content

Optional Parameters For 'func' ?


Recommended Posts

So i wrote a little function to find 'free' temp Filenames

func FileTempName ($dir,$ext)
  if $dir == "" Then $dir = @TempDir
  if FileExists($dir) == 0 Then $dir = @TempDir
  if StringRight($dir,1) <> '\' Then $dir = $dir & '\'
  if $ext == "" Then $ext="tmp"
  $timestamp = TimerStart()
     do
    $tempfile = filegetshortname($dir&Int(Random(10000000,99999999))&"."&$ext)
    if TimerStop($timestamp) > 2500 Then
    seterror(1)
    Return
    endif
     until FileExists($tempfile) == 0
  Return $tempfile
endfunc

and i want $dir and $ext to be optional.

is this possible?

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

No, but you could pass an array to the function. Then do some array checking to see how many optional paramaters have been passed through the array. Something like

Func OptionalParams($required, $required2, $optional)
  If UBound($optional) = 2 Then;2 vars passed through optional array
  ;do stuff
  ElseIf UBound($optional) = 0 Then;no vars passed through array
  ;do other stuff
  Else;general clause to catch bad input
  ;quit with some error message about bad paramaters
  EndIf
EndFunc

Edited to correct a typo in the code

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Optional paramters is a feature being discussed.

Antother workaround is to use a single string parameter where variables are separated by a the pipe "|" symbol. I use pipe here since it is never allow in file names or paths.

func FileTempName ($pipeSeparatedParams)
   Local $dir = "", $ext = "", $timestamp, $tempfile
   Local $params = StringSplit($pipeSeparatedParams, "|")
   If IsArray($params)Then
      If 1 = $params[0]  Then $dir = $params[1]
      If 2 = $params[0]  Then $ext = $params[2]
   EndIf
; rest of your code goes here

P.S. You might consider using FileFindNextFile.

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Still the goal remains unreachable... :)

But i can call it by using FileTempName('','') anyway :lol:

why shuld i consider using FileFindNextFile ?

i dont get the point here :D

thx for the nice support ( i think i will have to put this in my sig :huh2: )

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

Having the paramater be empty is not really that big of a problem. You could always do 'Function(0)' and have the function first test for a string (and exit if it doesn't get one, or whatever you need it to.) As long as you know that your function treats a non-string as no paramater it will work fine. Or use a blank string 'Function("")'. No need for the comma in your above example.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

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...