Sign in to follow this  
Followers 0
Ayke

Somethings Wrong

7 posts in this topic

#1 ·  Posted (edited)

The following Syntax.

CODE
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)

Endfunc

func check_application()

If ProcessExists($process) Then

$application = 1

Else

$application = 0

Endif

Endfunc

func statusrefreshvalue()

$file = FileOpen($statusfile, 2)

FileWriteLine ( $statusfile, "Netzwerk:" & $networkcheck & @CRLF )

FileWriteLine ( $statusfile, "Anwendung:" & @CRLF )

Endfunc

check_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 by m3rry

www.m3rry.de | nach müde kommt blöd

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 = 1

If $var Then; also possible: If @error = 0 Then ...

$networkcheck = $var

Else

$networkcheck = $var

EndIf

Return $networkcheck

Endfunc

And then:

Msgbox(0,"Network",Check_Network())

The function will return networkcheck right into your msgbox.

Edited by Stalker0

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

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  
Followers 0