Jump to content

AutoIt Error "Error: Variable used without being declared"


dRsrb
 Share

Recommended Posts

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

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

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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
Link to comment
Share on other sites

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