Ayke Posted October 31, 2007 Share Posted October 31, 2007 (edited) The following Syntax.CODEfunc check_network() $var = Ping($hostadress,250) If $var Then; also possible: If @error = 0 Then ... $networkcheck = $var Else $networkcheck = $var EndIfMsgbox(0,"Network",$networkcheck)Endfuncfunc check_application() If ProcessExists($process) Then $application = 1 Else $application = 0 EndifEndfuncfunc statusrefreshvalue() $file = FileOpen($statusfile, 2) FileWriteLine ( $statusfile, "Netzwerk:" & $networkcheck & @CRLF ) FileWriteLine ( $statusfile, "Anwendung:" & @CRLF )Endfunccheck_network()check_application()statusrefreshvalue()I get an error, because the Variable $networkcheck which comes out of the function is not declared, but i got the MSGBOX before with the set Value ... cant i declare a Variable in a function call the function and the variable is set ? Sounds weird, but im not that professional in coding Edited October 31, 2007 by m3rry www.m3rry.de | nach müde kommt blöd Link to comment Share on other sites More sharing options...
Nahuel Posted October 31, 2007 Share Posted October 31, 2007 Try putting this line at the top of the script: Global $networkcheck You are using that variable inside two different functions. From help file: If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends. Link to comment Share on other sites More sharing options...
Ayke Posted October 31, 2007 Author Share Posted October 31, 2007 Try putting this line at the top of the script: Global $networkcheck You are using that variable inside two different functions. From help file: thank you, it works www.m3rry.de | nach müde kommt blöd Link to comment Share on other sites More sharing options...
Stalker0 Posted October 31, 2007 Share Posted October 31, 2007 (edited) In autoit, functions can manipulate the value of a variable already created, or create local copies themselves.If you create a local copy, the variable will not survive past the function's end unless the variable is declared a global variable in the function, or its declared outside the function.There's a few ways to fix your problem:1) change $networkcheck = $var to GLOBAL $networkcheck = $var. That will fix the problem, although if you want to use the variable for something else that's not ideal, and its not good practice to put a lot of global variables in functions.2) This is my preferred way.func check_network()$var = 1If $var Then; also possible: If @error = 0 Then ...$networkcheck = $varElse$networkcheck = $varEndIfReturn $networkcheckEndfuncAnd then:Msgbox(0,"Network",Check_Network())The function will return networkcheck right into your msgbox. Edited October 31, 2007 by Stalker0 Link to comment Share on other sites More sharing options...
Monamo Posted October 31, 2007 Share Posted October 31, 2007 thank you, it works Another route would be to set the variable during the function call: $networkcheck = check_network() MsgBox(0, "Network - Outside of function", $networkcheck) Func check_network() $var = Ping($hostadress, 250) If $var Then; also possible: If @error = 0 Then ... $networkcheck = $var Else $networkcheck = $var EndIf MsgBox(0, "Network", $networkcheck) Return $networkcheck EndFunc ;==>check_network In this usage, just be sure to use Return (last line of the function example) in order to have the value passed back to your variable. - MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup] Link to comment Share on other sites More sharing options...
Blue_Drache Posted October 31, 2007 Share Posted October 31, 2007 Manipulation of global varaiables inside functions without passing ByRef is a bad habit and can cause a headache later on. Global $netcheck $netcheck = _pingCheck($netcheck) Msgbox $netcheck Func _pingCheck(ByRef $var) <blah blah blah> Return $var EndFunc Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
Ayke Posted October 31, 2007 Author Share Posted October 31, 2007 Cheers, never thought there would be that much reaction and that fast, youre awesome guys Thanks for the explanation, using the return Function now, works properly and is more logical to me 2 TY www.m3rry.de | nach müde kommt blöd Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now