ahha Posted February 25, 2023 Posted February 25, 2023 I've whittled down a program to illustrate where my problem is. I've been staring at the code and simply am missing something that when I see it pointed out to me will be a DUH moment. On line 14 I declare $file a Global and init it to null "". Line 37 has a Case $idFileMenu_Open where I invoke $file = FileOpenDialog at line 52. When I return to the Case statement $file is null ("") instead of a filename. Running the program (and selecting any .txt file) will message out the $file value at each point. Any comments welcome as I'm stumped expandcollapse popup;author: ahha - not so bright :( ;file: Open File issue v1e.au3 #AutoIt3Wrapper_run_debug_mode=Y ;use this to debug in console window <--- LOOK, ;RECALL ;debug will toggle it on/off so use: ; debug if needed Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration #include <File.au3> #include <GUIConstantsEx.au3> #include <GUIEdit.au3> #include <WindowsConstants.au3> Global $data = "" Global $file = "" ;is the full path filename like 'C:\Program Files (x86)\AutoIt3\...\123.txt' Global $cIDFormTextEditorTitle = "* Untitled" ;init value ;==== set up the GUI and the Edit control ==== Global $hFormTextEditor = GUICreate($cIDFormTextEditorTitle, 920, 570, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX), $WS_EX_CLIENTEDGE) ;handle Global $cIDTextEdit = GUICtrlCreateEdit("", -2, -2, 921, 528, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL, $ES_NOHIDESEL)) ;control ID ;-2 line up left, -2 line up top, +1 width, +1 bottom GUISetState(@SW_SHOW, $hFormTextEditor) ;display the GUI ;File Menu Local $idFileMenu = GUICtrlCreateMenu("&File") ;Create the file menu. This is the main menu and will have the sub-menus. Underline F when ALT is pressed. ;Main menu ;sub-menus Local $idFileMenu_Open = GUICtrlCreateMenuItem("&Open", $idFileMenu) ;Create the "Open" menu item. _GUICtrlEdit_SetText($cIDTextEdit, $data) ;display any file contents read in from like a command line filename Local $nMsg ;MAIN loop While 1 $nMsg = GUIGetMsg() ;Check what's happening in the GUI (Graphical User Interface) If $nMsg <> 0 Then ;we have a message so Switch $nMsg ;Check for GUI events ;Check File menu --- Case $idFileMenu_Open ;Open menu item clicked, or shortcut keys Ctrl+O = Open pressed Pause("Start: Case $idFileMenu_Open $file = '" & $file & "'") _Open("") ;let user pick it if $file = "" Pause("End: Case $idFileMenu_Open $file = '" & $file & "'") Case $GUI_EVENT_CLOSE Exit EndSwitch ;End running through GUI events. EndIf WEnd ;End main body loop ;File menu functions ======== Func _Open($file) ;_Open("") ;let user pick it v2cd Pause("In: Func _Open($file), $file = '" & $file & "'") If $file = "" Then ;let user pick the file $file = FileOpenDialog("Open",@WorkingDir,"text (*.txt)| all (*.*)") ;Start an open file dialog. Define text files (*.txt) as one filter, define all files (*.*) as a second filter. Pause("$file = '" & $file & "'") EndIf $data = FileRead($file) ;Read the data of the file _GUICtrlEdit_SetText($cIDTextEdit, $data) ;Set the data to be displayed $cIDFormTextEditorTitle = _GetShortFileName($file) ;set title with short filename WinSetTitle($hFormTextEditor, "", $cIDFormTextEditorTitle) Pause($file &@CRLF& "Read into editor.") EndFunc ;Func _Open() ;misc functions ======== Func _GetShortFileName($sfullfilename) Local $sDrive, $sDir, $sFileName, $sExtension ;for use by _PathSplit Local $aPathSplit = _PathSplit($sfullfilename, $sDrive, $sDir, $sFileName, $sExtension) Return($aPathSplit[3] & $aPathSplit[4]) ;return the short filename EndFunc ;Func _GetShortFileName($sfullfilename) Func Pause($text="") ;__scrolltext("Debug: Paused " & $text & @CRLF) MsgBox(262144, "DEBUG", "Paused: " & $text) EndFunc ;Func Pause($text="")
Danp2 Posted February 25, 2023 Posted February 25, 2023 28 minutes ago, ahha said: Func _Open($file) This creates a local variable named $file, so your global variable is never updated. You will either need to rename this variable or restructure your function so that it returns a value that can then be assigned to the global $file. ahha 1 Latest Webdriver UDF Release Webdriver Wiki FAQs
ahha Posted February 25, 2023 Author Posted February 25, 2023 @Danp2I don't understand how Func _Open creates a local. I've assigned a Global $file to receive the user picked filename. Can you explain further which functions create a local?
ahha Posted February 25, 2023 Author Posted February 25, 2023 @Danp2Now I understand. By using _Open($file) $file became a local. By changing it to _Open($xfile) then everything works. For reference code below. Thanks - it was a DUH moment. expandcollapse popup;author: ahha - not so bright :( ;file: Open File issue v1e.au3 #AutoIt3Wrapper_run_debug_mode=Y ;use this to debug in console window <--- LOOK, ;RECALL ;debug will toggle it on/off so use: ; debug if needed Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration #include <File.au3> #include <GUIConstantsEx.au3> #include <GUIEdit.au3> #include <WindowsConstants.au3> Global $data = "" Global $file = "" ;is the full path filename like 'C:\Program Files (x86)\AutoIt3\...\123.txt' Global $cIDFormTextEditorTitle = "* Untitled" ;init value ;==== set up the GUI and the Edit control ==== Global $hFormTextEditor = GUICreate($cIDFormTextEditorTitle, 920, 570, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX), $WS_EX_CLIENTEDGE) ;handle Global $cIDTextEdit = GUICtrlCreateEdit("", -2, -2, 921, 528, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL, $ES_NOHIDESEL)) ;control ID ;-2 line up left, -2 line up top, +1 width, +1 bottom GUISetState(@SW_SHOW, $hFormTextEditor) ;display the GUI ;File Menu Local $idFileMenu = GUICtrlCreateMenu("&File") ;Create the file menu. This is the main menu and will have the sub-menus. Underline F when ALT is pressed. ;Main menu ;sub-menus Local $idFileMenu_Open = GUICtrlCreateMenuItem("&Open", $idFileMenu) ;Create the "Open" menu item. _GUICtrlEdit_SetText($cIDTextEdit, $data) ;display any file contents read in from like a command line filename Local $nMsg ;MAIN loop While 1 $nMsg = GUIGetMsg() ;Check what's happening in the GUI (Graphical User Interface) If $nMsg <> 0 Then ;we have a message so Switch $nMsg ;Check for GUI events ;Check File menu --- Case $idFileMenu_Open ;Open menu item clicked, or shortcut keys Ctrl+O = Open pressed Pause("Start: Case $idFileMenu_Open $file = '" & $file & "'") _Open("") ;let user pick it if $file = "" Pause("End: Case $idFileMenu_Open $file = '" & $file & "'") Case $GUI_EVENT_CLOSE Exit EndSwitch ;End running through GUI events. EndIf WEnd ;End main body loop ;File menu functions ======== Func _Open($xfile) ;_Open("") ;let user pick it v2cd Pause("In: Func _Open($file), $file = '" & $file & "'") If $xfile = "" Then ;let user pick the file $file = FileOpenDialog("Open",@WorkingDir,"text (*.txt)| all (*.*)") ;Start an open file dialog. Define text files (*.txt) as one filter, define all files (*.*) as a second filter. Pause("$file = '" & $file & "'") EndIf $data = FileRead($file) ;Read the data of the file _GUICtrlEdit_SetText($cIDTextEdit, $data) ;Set the data to be displayed $cIDFormTextEditorTitle = _GetShortFileName($file) ;set title with short filename WinSetTitle($hFormTextEditor, "", $cIDFormTextEditorTitle) Pause($file &@CRLF& "Read into editor.") EndFunc ;Func _Open() ;misc functions ======== Func _GetShortFileName($sfullfilename) Local $sDrive, $sDir, $sFileName, $sExtension ;for use by _PathSplit Local $aPathSplit = _PathSplit($sfullfilename, $sDrive, $sDir, $sFileName, $sExtension) Return($aPathSplit[3] & $aPathSplit[4]) ;return the short filename EndFunc ;Func _GetShortFileName($sfullfilename) Func Pause($text="") ;__scrolltext("Debug: Paused " & $text & @CRLF) MsgBox(262144, "DEBUG", "Paused: " & $text) EndFunc ;Func Pause($text="")
mistersquirrle Posted February 25, 2023 Posted February 25, 2023 (edited) Yes, if you name a variable as a function parameter or Local the same name as a Global variable, the function will use your Local version instead, and will not access/update/change/whatever to the Global variable. The Global variable basically will not exist while that function runs. I recommend that you check out this page: https://www.autoitscript.com/wiki/Best_coding_practices It has some naming tips for variables, such as $g_iSomeVar for a Global variable, and $iSomeVar for the local version. Your change of $xfile works, but what is x? Does it have any meaning? If it works for you, great, otherwise going along with a standard is a good idea, if you can (IMO). Edited February 25, 2023 by mistersquirrle ahha 1 We ought not to misbehave, but we should look as though we could.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now