Jump to content
john_2019

To pass variable from one function to another

Recommended Posts

How to pass a variable from one function to another? I tried different ways like this one:

#include <MsgBoxConstants.au3>

Func CopyToPseudoClipboard()
    Local $string = "string"
EndFunc

Func _Test($string)
    MsgBox($MB_SYSTEMMODAL, "title", $string)
EndFunc

HotKeySet("+{Enter}", "_Test")

While 1
    Sleep(100)
WEnd

Also tried global variables and Return. Neither works. Could somebody show me how to use it?

Share this post


Link to post
Share on other sites
Posted (edited)

There are a number of ways to do this, it really depends if you wish to use the variable throughout all your functions for example:

Global $g_sString = ""

;~ Set $g_sString value
_SetVariable("Example1 String")

;~ Pass the global variable as a parameter
_Example1($g_sString)

;~ $g_sString is referenced within the _Example2 function
_Example2()

Func _SetVariable($_sString)
    ;~ $_sString is local to this function
    ;~ $g_sString has already been decalared in the global scope
    $g_sString = $_sString
EndFunc

Func _Example1($_sString = "")
    MsgBox(4096, "Example 1", "$_sString = " & $_sString)
EndFunc

Func _Example2()
    _SetVariable("Example2 String")
    MsgBox(4096, "Example 2", "$g_sString = " & $g_sString)
EndFunc

Edit: Added a couple of different examples

Edited by Subz

Share this post


Link to post
Share on other sites
Posted (edited)

@Subz Your examples are really useful, I'm very appreciated. It seems both of them utilize global variable, right? As I know, global variables are generally discouraged (except special cases). Could you also show the way without globals?

Edited by john_2019

Share this post


Link to post
Share on other sites

Global variables are fine, especially if you wish to use the variable in multiple functions.  You can also use functions with parameters (see _Example1 above, but as mikell mentioned if you wish to use HotKeySet, you would need to use a global variable or call a function within your HotKeySet function.  Hope that made sense.

Share this post


Link to post
Share on other sites

Do not forget that there is Local Static $Variables.  If you only need that var in a single function, but wants it to act like a global, think of it.  I believe it is one of the underestimated and underused feature of AutoIt...

Share this post


Link to post
Share on other sites

Byref has nothing to do with that.

I second what @Nine said about Static variables.  A function can easily be used both explicitely, on timeout and by hotkey and behave differently in all cases.

HotKeySet("{F2}", fct)
HotKeySet("{ESC}", stop)

AdlibRegister(rst, 14000)       ; reset static variable every 14s

Func fct($p)
    Local Static $v = 26
    If IsDeclared("p") Then
        If $p < 0 Then
            ConsoleWrite("fct invoked on timeout;  value = " & $v & " and will be reset to 0" & @LF)
            $v = 0
        Else
            ConsoleWrite("fct invoked explicitely; value = " & $v & " and will increment by " & $p & @LF)
            $v += $p
        EndIf
    Else
        ConsoleWrite("fct invoked thru hotkey; value = " & $v & " and will increment by " & 100 & @LF)
        $v += 100
    EndIf
EndFunc

Func stop()
    Exit
EndFunc

Func rst()
    fct(-1)
EndFunc

While 1
    Sleep(1000)
    fct(Random(1, 9, 1))
WEnd

If you need to pass any value in $p (meaning that you can't differentiate calling method by looking at $p), then declare a second variable in fct() and pass there a value for switching between wanted behaviors.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Posted (edited)

This is what I'm trying to create:

Global $g_PseudoClipboard = ""

Func _CopyToPseudoClipboard()
    Local $OriginalClipboard = ClipGet()
    Send("^c")
    $g_PseudoClipboard = ClipGet()
    ClipPut($OriginalClipboard)
EndFunc

Func _ShowPseudoClipboard()
    _CopyToPseudoClipboard()
    MsgBox(4096, "", $g_PseudoClipboard)
EndFunc

HotKeySet("+{Enter}", "_ShowPseudoClipboard")

While 1
    Sleep(100)
WEnd

Launch it and then open Windows file manager (that is, Windows Explorer), and press Shift+Enter. The currently selected file/directory will be copied to the variable and then the corresponding message box will show it.

What I don't understand is why it works in the following way: Works → Then doesn't work → Then works → Then doesn't work → And so on. ?

Edited by john_2019

Share this post


Link to post
Share on other sites
Posted (edited)

You might avoid a global var by using Return
And also include some Sleep() so your system can breathe a little  :)

HotKeySet("+{Enter}", "_ShowPseudoClipboard")

While 1
    Sleep(100)
WEnd

Func _ShowPseudoClipboard()
    $PseudoClipboard = _CopyToPseudoClipboard()
    ;MsgBox(4096, "", $PseudoClipboard)
   consolewrite("result = " & $PseudoClipboard & @crlf & @crlf)
EndFunc

Func _CopyToPseudoClipboard()
    $OriginalClipboard = ClipGet()
    Sleep(10)
    Send("^c")
    Sleep(10)
    Local $NewClipboard = ClipGet()
; check it
consolewrite("$NewClipboard = " & $NewClipboard & @crlf)
consolewrite("$OriginalClipboard = " & $OriginalClipboard & @crlf)
    ClipPut($OriginalClipboard)
    Return $NewClipboard
EndFunc

 

Edited by mikell

Share this post


Link to post
Share on other sites

This is one of the rare uses of IsDeclared().


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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