Jump to content
Sign in to follow this  
Lee Evans

Returning A Variable From A Function

Recommended Posts

Hope some one can help. I am reading a file line by line looking for a string that contains a certain token. As I will be looking for different tokens in the same file I decided to make my own function that returns the second part of the line containing a required path as a variable.

Trouble is I can't seem to get the function to return the variable.

What am I doing wrong?

AutoItSetOption("TrayIconDebug", 1)

Local $spath="dfdf"
Local $userdir="C:\test\"

_FilePathFromSysParams ($userdir, "SYSDATAPATH", $spath )

Func _FilePathFromSysParams ($userdir, $token, ByRef $spath)
Local $line = 1
Local $read = 0
Local $stringpresent = 0

For $stringpresent = 0 to 1
    $read = FileReadLine ($userdir&"file.txt",$line)
    MsgBox(4096, "Data directory", ""& $read)
    $stringpresent = StringInStr( $read, $token );This will return 1 if $token is present
    $line = $line +1
    If $stringpresent <> 1 Then ContinueLoop

$linesplit = StringSplit($read, ","); in the file each line is "token,path"
$linesplit[2] = $spath
Return $spath
EndFunc; _FilePathFromSysParams

$datadir = $spath
MsgBox(4096, "Data directory", ""& $datadir)






            Share this post
            Link to post

                Share on other sites






















                Hmmm .. and what have we here?



            Active Members


                936 posts






Looks like you're ignoring the return value   . See the following illustration:
   $nValue1 = 100
    $nValue2 = 10

;This just causes the code in the UDF to be executed
;.. but ignores any return value
    _AddThem($nValue1, $nValue2)    
;This executes the code in the UDF
;.. and does something with the return value..
    $nReturnValue = _AddThem($nValue1, $nValue2)    
    MsgBox(4096 + 32, "", $nReturnValue)    

Func _AddThem ($pnV1, $pnV2)
;This function adds two numbers, and returns the result

    $nSum = $pnV1 + $pnV2
    Return $nSum


Hope this helps :huh2:

Share this post

Link to post
Share on other sites

Since your function has definded $spath as BYREF, the value can be changed from the function itself, so there is no RETURN necessary.

I'd suggest changing

$linesplit[2] = $spath
Return $spath


$spath = $linesplit[2]

which should do the job.

And I'd move the commands below the EndFunction above the whole Function block to make it more readable :D

It's my job to comfort the disturbed and to disturb the comfortable.
My Projects: Profiler, MakeSFX, UserInfo, Simple Robocopy Progressbar

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...