Jump to content

Somethings Wrong


Ayke
 Share

Recommended Posts

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

Link to comment
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.

Link to comment
Share on other sites

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
Link to comment
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]

Link to comment
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

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