Sign in to follow this  
Followers 0
dirty

how to end func within func ?

12 posts in this topic

Stuck with my code and need to end function if @error

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#RequireAdmin
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
IsAdmin ( )


$Form1 = GUICreate("hosts Editor",200,440)
$hosts = (@WindowsDir & "\System32\drivers\etc\hosts")
$OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1)
$fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts")
GUICtrlCreateLabel ('Example:',0,6,42)
GUICtrlCreateinput ("127.0.0.1 www.google.com",43,3,157)
$Input = GUICtrlCreateEdit ($fileLocalRead,0,30,200,370)
$Button = GUICtrlCreateButton ("Save",0,400,100,40)
$ButtonCancel = GUICtrlCreateButton ("Close",100,400,100,40)

TrayCreateItem('Modify "HOSTS"')
TrayItemSetOnEvent(-1,"ModifyFunction") ;function name
TrayCreateItem('Exit')
TrayItemSetOnEvent(-1,"ExitFunction") ;function name
TraySetState()

While 1
;empty loop
WEnd

Func ModifyFunction() ;check if file exist localy. If not, copy it to local and only then start reading it and displaying it in edit box
$checkIfLocalExist = FileExists (@ScriptDir & "\Saved\hosts") ;Check if hosts is previously saved into saved folder
If $checkIfLocalExist = 0 Then ;if not, then
$CopyHostsToLocal = FileCopy ($hosts,@ScriptDir & "\Saved\hosts",0+8) ;copy hosts from system to saved folder
If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")
Escape() ;Go to end of the function is error
EndIf

$hosts = (@WindowsDir & "\System32\drivers\etc\hosts")
$fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts")
GUICtrlSetData ($Input, $fileLocalRead)
$OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1)
GUISetState(@SW_SHOW, $Form1)

While 1
   $msg   = GUIGetMsg()
   Switch $msg
       Case $GUI_EVENT_CLOSE
           GUISetState(@SW_HIDE, $Form1)
           ExitLoop
       Case $Button
           FileOpen (@ScriptDir & "\Saved\hosts", 2)
           $readAgain = GUICtrlRead ($Input)
           $write = FileWrite ($OpenFile, $readAgain)
        FileClose ($OpenFile)
        $replacehost = FileCopy (@ScriptDir & "\Saved\hosts", @WindowsDir & "\System32\drivers\etc\hosts", 1)
        If $write = 1 and $replacehost = 1 Then
        MsgBox(0,'','Saved')
        Else
        MsgBox(0,'','Error writing')
        Endif
            Case $ButtonCancel
        GUISetState(@SW_HIDE, $Form1)
           ExitLoop
   EndSwitch
WEnd

Func Escape() ;end of the function
    MsgBox(0,'','Try Again')
EndFunc

EndFunc

Func ExitFunction()
Exit
EndFunc

As soon as i added

If @error
Escape() ;Go to end of the function is error
Endif

Func Escape() ;end of the function
    MsgBox(0,'','Try Again')
EndFunc

i get error saying: syntax error

Func

Why ? Do i have limits naming functions or something ?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You can't define functions in a function. You should bring the Func declaration outside.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#RequireAdmin
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
IsAdmin ( )


$Form1 = GUICreate("hosts Editor",200,440)
$hosts = (@WindowsDir & "\System32\drivers\etc\hosts")
$OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1)
$fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts")
GUICtrlCreateLabel ('Example:',0,6,42)
GUICtrlCreateinput ("127.0.0.1 www.google.com",43,3,157)
$Input = GUICtrlCreateEdit ($fileLocalRead,0,30,200,370)
$Button = GUICtrlCreateButton ("Save",0,400,100,40)
$ButtonCancel = GUICtrlCreateButton ("Close",100,400,100,40)

TrayCreateItem('Modify "HOSTS"')
TrayItemSetOnEvent(-1,"ModifyFunction") ;function name
TrayCreateItem('Exit')
TrayItemSetOnEvent(-1,"ExitFunction") ;function name
TraySetState()

While 1
;empty loop
WEnd

Func ModifyFunction() ;check if file exist localy. If not, copy it to local and only then start reading it and displaying it in edit box
    $checkIfLocalExist = FileExists (@ScriptDir & "\Saved\hosts") ;Check if hosts is previously saved into saved folder
    If $checkIfLocalExist = 0 Then ;if not, then
        $CopyHostsToLocal = FileCopy ($hosts,@ScriptDir & "\Saved\hosts",0+8) ;copy hosts from system to saved folder
        If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")
        Escape() ;Go to end of the function is error
    EndIf

    $hosts = (@WindowsDir & "\System32\drivers\etc\hosts")
    $fileLocalRead = FileRead(@ScriptDir & "\Saved\hosts")
    GUICtrlSetData ($Input, $fileLocalRead)
    $OpenFile = FileOpen (@ScriptDir & "\Saved\hosts", 0+1)
    GUISetState(@SW_SHOW, $Form1)

    While 1
        $msg   = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                GUISetState(@SW_HIDE, $Form1)
                ExitLoop
            Case $Button
                FileOpen (@ScriptDir & "\Saved\hosts", 2)
                $readAgain = GUICtrlRead ($Input)
                $write = FileWrite ($OpenFile, $readAgain)
                FileClose ($OpenFile)
                $replacehost = FileCopy (@ScriptDir & "\Saved\hosts", @WindowsDir & "\System32\drivers\etc\hosts", 1)
                If $write = 1 and $replacehost = 1 Then
                    MsgBox(0,'','Saved')
                Else
                    MsgBox(0,'','Error writing')
                Endif
            Case $ButtonCancel
                GUISetState(@SW_HIDE, $Form1)
                ExitLoop
        EndSwitch
    WEnd
EndFunc

Func Escape() ;end of the function
    MsgBox(0,'','Try Again')
EndFunc

Func ExitFunction()
    Exit
EndFunc

EDIT: Also learn to use proper indentation for nested statements so your code is not an eyesore to look at and a brain twister to read.

Edited by omikron48

Share this post


Link to post
Share on other sites

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Either way, much simpler than what you tried :|

Edited by Minikori

For those who are asking questions, look in the help file first. I'm tired of people asking stupid questions about how to do things when 10 seconds in the help file could solve their problem.[quote name='JRowe' date='24 January 2010 - 05:58 PM' timestamp='1264381100' post='766337'][quote name='beerman' date='24 January 2010 - 03:28 PM' timestamp='1264372082' post='766300']They already have a punishment system for abuse.[/quote]... and his his name is Valik.[/quote]www.minikori.com

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

EDIT: Also learn to use proper indentation for nested statements so your code is not an eyesore to look at and a brain twister to read.

What do you mean, variable names or what ? I thought variable names can be anything i want :D

Then where do i get appropriate names ? or am i doing something else wrong ?

If @error Return SetError (1, 2, 3) ?

then what happens ? Would it jump to the end of current function ?

Thanks guys

Edited by dirty

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

If @error Return SetError (1, 2, 3) ?

then what happens ? Would it jump to the end of current function ?

It returns to the statement that called this Func. Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I meant follow coding convention when you post code online, like indenting lines depending on their nest level in the code so it's easier to follow.

Kinda like:

If <condition> Then
    <statement1>
    ...
    <statementn>
EndIf

Or

If <condition1> Then
    <statement1>
    If <condition2> Then
        <statementA>
        ...
        <statementZ>
    EndIf
    ...
    <statementn>
EndIf

Doing so makes it clear which lines of code are inside conditionals, loops or function declarations.

Edited by omikron48

Share this post


Link to post
Share on other sites

I meant follow coding convention when you post code online, like indenting lines depending on their nest level in the code so it's easier to follow.Doing so makes it clear which lines of code are inside conditionals, loops or function declarations.

Ah ok, i thought something else was wrong. :D

Its just the way i like thing, all aligned to the left but OK

Share this post


Link to post
Share on other sites

It returns to the statement that called this Func.

Actually it would fail entirely with an error because of the missing "Then"

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Actually it would fail entirely with an error because of the missing "Then"

hhmm interestin.

You mean it would jump back to

If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")

? sorry im not sure what statement is :D

Share this post


Link to post
Share on other sites

hhmm interestin.

You mean it would jump back to

If @error Then MsgBox (16,"ERROR","Error Copying hosts File, it must be in use")

? sorry im not sure what statement is :D

What you wrote in this post is correct. Your first post had left out the "Then" keyword as in

if @Error Return..Which will fail and throw a syntax error


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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