Sign in to follow this  
Followers 0
dRsrb

AutoIt Error "Error: Variable used without being declared"

7 posts in this topic

Hi everybody! :)

I'm new to autoit and I like it VERY much!

But I have a problem, that I don't understand.

I extract the following code segment out of my application:

#include <GUIConstantsEx.au3>

AutoItSetOption('GUIOnEventMode', 1)
;AutoItSetOption('MustDeclareVars', 1)

Global Const $ApplicationTitle      = 'Test'
Global Const $DefaultTestDirectory  = 'C:\'
Global $Main
Global $TestDirectoryInput, $CurrentTestDirectory

_Main()

Func _Main()
    
; GUI: Main window
    $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication')

    $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43)
    
        GUICtrlCreateLabel('Test', 17, 19, 38, 17)
        
        $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19)
        GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED)
        
        $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0)
        GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc')
        
    $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1)
    
    GUISetState(@SW_SHOW)
    
;   _TestFunc()
    
    While True
        Sleep(1000)
    WEnd
    
;   GUIDelete()
    
EndFunc

Func _ExitApplication()

    If @GUI_WINHANDLE = $Main Then
        Exit 0
    EndIf
    
EndFunc

Func _TestFunc($sTestDirectory = $CurrentTestDirectory)
    
    MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory)
    MsgBox(0, '$sTestDirectory', $sTestDirectory)
    
EndFunc

The Problem is when I press the 'Test'-Button and call my 'TestFunc'-function I get an AutoIt Error - "Variable used without being declared".

But the variable is declared in the signature of the function.

Hmmm, so why does 'GUICtrlSetOnEvent' have another behaviour then calling 'TestFunc()' somewhere else?

2nd question: Do I need 'GUIDelete()' when 'AutoItSetOption('GUIOnEventMode', 1)' is?

Bye

dRsrb

Share this post


Link to post
Share on other sites



@dRsrb

#include <GUIConstantsEx.au3>

AutoItSetOption('GUIOnEventMode', 1)
;AutoItSetOption('MustDeclareVars', 1)

Global $ApplicationTitle = 'Test'
Global $DefaultTestDirectory = 'C:\'
Global $Main
Global $TestDirectoryInput, $CurrentTestDirectory

_Main()

Func _Main()

    ; GUI: Main window
    $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication')

    $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43)

    GUICtrlCreateLabel('Test', 17, 19, 38, 17)

    $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19)
    GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED)

    $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0)
    GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc')

    $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1)

    GUISetState(@SW_SHOW)

    ;    _TestFunc()

    While True
        Sleep(1000)
    WEnd

    ;    GUIDelete()

EndFunc   ;==>_Main

Func _ExitApplication()

    If @GUI_WinHandle = $Main Then
        Exit 0
    EndIf

EndFunc   ;==>_ExitApplication

Func _TestFunc()
    $sTestDirectory = $CurrentTestDirectory
    MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory)
    MsgBox(0, '$sTestDirectory', $sTestDirectory)
EndFunc   ;==>_TestFunc

2nd question: Do I need 'GUIDelete()' when 'AutoItSetOption('GUIOnEventMode', 1)' is?

Look at GuiSetState and @SW_HIDE

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

dRsrb,

Answer to first question: You cannot send parameters when using the OnEvent mode - it is the main limitation. However, you might like to look here where martin has posted a UDF to get over this. I have not yet played with this - on my To-Do list somewhere!

Answer to second question: When you Exit, all GUIs are deleted automatically, so your _ExitApplication() function will delete the GUI for you. This behaviour is independent of GUI mode. Where you have GUIDelete() will never be reached by your code anyway - but I am sure you realise that.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Problem was, that you called the function with guionevent which doesn't permit function calling with params.

#include <GUIConstantsEx.au3>

AutoItSetOption('GUIOnEventMode', 1)
;AutoItSetOption('MustDeclareVars', 1)

Global Const $ApplicationTitle      = 'Test'
Global Const $DefaultTestDirectory  = 'C:\'
Global $Main
Global $TestDirectoryInput, $CurrentTestDirectory

_Main()

Func _Main()
    
; GUI: Main window
    $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication')

    $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43)
    
        GUICtrlCreateLabel('Test', 17, 19, 38, 17)
        
        $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19)
        GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED)
        
        $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0)
        GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc')
        
    GUISetState(@SW_SHOW)
    
;   _TestFunc()
    
    While True
        Sleep(1000)
    WEnd
    
;   GUIDelete()
    
EndFunc

Func _ExitApplication()

    If @GUI_WINHANDLE = $Main Then
        Exit 0
    EndIf
    
EndFunc

Func _TestFunc()
    $sTestDirectory = GUICtrlRead($TestDirectoryInput)
    $CurrentTestDirectory = $sTestDirectory 
    MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory)
    MsgBox(0, '$sTestDirectory', $sTestDirectory)
EndFunc

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you very much guys!

So I will need to call a intermediate function to declare and to finally call my '_TestFunc()'! :)

#include <GUIConstantsEx.au3>

AutoItSetOption('GUIOnEventMode', 1)
;AutoItSetOption('MustDeclareVars', 1)

Global Const $ApplicationTitle      = 'Test'
Global Const $DefaultTestDirectory  = 'C:\'
Global $Main
Global $TestDirectoryInput, $CurrentTestDirectory

_Main()

Func _Main()
    
; GUI: Main window
    $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication')

    $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43)
    
        GUICtrlCreateLabel('Test', 17, 19, 38, 17)
        
        $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19)
        GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED)
        
        $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0)
        GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc_Temp')
        
    $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1)
    
    GUISetState(@SW_SHOW)
    
    _TestFunc()
    
    While True
        Sleep(1000)
    WEnd
    
EndFunc

Func _ExitApplication()

    If @GUI_WINHANDLE = $Main Then
        Exit 0
    EndIf
    
EndFunc

Func _TestFunc_Temp()
    
    $sTestDirectory = $CurrentTestDirectory
    _TestFunc(sTestDirectory)
    
EndFunc

Func _TestFunc($sTestDirectory = $CurrentTestDirectory)
    
    MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory)
    MsgBox(0, '$sTestDirectory', $sTestDirectory)
    
EndFunc

Great! Thank you! :)

Bye dRsrb

Edited by dRsrb

Share this post


Link to post
Share on other sites

Easiest way is probably a Kafu suggests.

If you need to have parameters passed then as Melba23 mentioned, you could use a udf to do it. Here is an example using my onevent udf (link in my signature) which assumes the udf has been saved in the same folder as your script.

#include <GUIConstantsEx.au3>
#include "onEventFunc.au3"

AutoItSetOption('GUIOnEventMode', 1)
;AutoItSetOption('MustDeclareVars', 1)

Global Const $ApplicationTitle      = 'Test'
Global Const $DefaultTestDirectory  = 'C:\'
Global $Main
Global $TestDirectoryInput, $CurrentTestDirectory,$somedirectory = @ScriptDir

_Main()

Func _Main()
    
; GUI: Main window
    $Main = GUICreate($ApplicationTitle, 681, 48, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_ExitApplication')

    $DirectoriesControlGroup = GUICtrlCreateGroup('Directory', 4, 1, 673, 43)
    
        GUICtrlCreateLabel('Test', 17, 19, 38, 17)
        
        $TestDirectoryInput = GUICtrlCreateInput($DefaultTestDirectory, 78, 16, 549, 19)
        GUICtrlSetState($TestDirectoryInput, $GUI_DROPACCEPTED)
        
        $TestDirectoryButton = GUICtrlCreateButton('Test', 634, 15, 35, 21, 0)
       ;GUICtrlSetOnEvent($TestDirectoryButton, '_TestFunc',)
        SetOnEventA($TestDirectoryButton, '_TestFunc', $paramByRef,"$someDirectory")
        
    $CurrentTestDirectory = GUICtrlRead($TestDirectoryInput, 1)
    
    GUISetState(@SW_SHOW)
    
;   _TestFunc()
    
    While True
        Sleep(1000)
    WEnd
    
;   GUIDelete()
    
EndFunc

Func _ExitApplication()

    If @GUI_WINHANDLE = $Main Then
        Exit 0
    EndIf
    
EndFunc

Func _TestFunc($sTestDirectory = $CurrentTestDirectory)
    
    MsgBox(0, '$CurrentTestDirectory', $CurrentTestDirectory)
    MsgBox(0, '$sTestDirectory', $sTestDirectory)
    
EndFunc

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Thank you martin too!

Your UDF sounds interesting. I will definitely check it!

Bye

dRsrb

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