sulfurious Posted October 19, 2006 Author Posted October 19, 2006 I have another question. Say I have a script where there is a GUI, with $vars = controls. And, this script can also use command parameters, where some functions do not need the gui (like run from context menu). So, by starting from command line, the whole guicreate is passed over, and values are directly passed to functions. Inside the function, I have a boolean for parts that interact with gui. Unfortunately, when the compiler (I think) runs the app, those not created variables (that would be used if the GUI had been created) throw an error. Which stands to make sense. Is there a method to let the variables be there, though not created, as long as they are nested in a switch that will not allow them to be called? Is this possibly one example of why you would create all the GUI variables Global (or Enum them) before the start of the GUICreate? Seems a shame to have to declare variables that will not be used. Or am I just forced to create a function for GUI and a function for command parameters? advice welcome, Sul
JSThePatriot Posted October 20, 2006 Posted October 20, 2006 I have another question. Say I have a script where there is a GUI, with $vars = controls. And, this script can also use command parameters, where some functions do not need the gui (like run from context menu). So, by starting from command line, the whole guicreate is passed over, and values are directly passed to functions. Inside the function, I have a boolean for parts that interact with gui. Unfortunately, when the compiler (I think) runs the app, those not created variables (that would be used if the GUI had been created) throw an error. Which stands to make sense. Is there a method to let the variables be there, though not created, as long as they are nested in a switch that will not allow them to be called? Is this possibly one example of why you would create all the GUI variables Global (or Enum them) before the start of the GUICreate? Seems a shame to have to declare variables that will not be used. Or am I just forced to create a function for GUI and a function for command parameters? advice welcome, Sul This is what I use... If Not(IsDeclared("cI_CompName")) Then Global $cI_CompName = @ComputerName EndIf Pulled directly from my CompInfo.au3 Library. JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)
Valik Posted October 20, 2006 Posted October 20, 2006 (edited) Stupid code. Works but is very bad style. Read the remarks for IsDeclared().Edit: At least I assume it works. To be perfectly honest, I'm not sure what problem that code is supposed to solve. Edited October 20, 2006 by Valik
JSThePatriot Posted October 20, 2006 Posted October 20, 2006 Stupid code. Works but is very bad style. Read the remarks for IsDeclared(). Edit: At least I assume it works. To be perfectly honest, I'm not sure what problem that code is supposed to solve. I assume you are speaking of needing to use Assign, and Eval with the variable from this point on. Why would that need to be the case? I am using that code to make my CompInfo.au3 Library more modular, but still have standard defaults. Such as the following examples. ;Set different computer name $cI_CompName = "SOMEComp" #Include "CompInfo.au3"oÝ÷ Ù8^iº/yÊz+çߢ»m¢ëXÂ+zj-z¢~wuçH«Þ×â+!yÛayÊ'½êìè~Ø^&ëj¼¡·«zj-z®±¦åx¦º{.欶!È, Úßè¬KÚªÞç²Æ«ÊÚ.±ì!z{¬x°7ªÞv'í+¢é]mçî®Ø^ìi¨§yÛazr0¢é]z0'«®È~z-¶§!©¬¶)e{ayÖ«'£ajÛayú%"(uéÉ·zÛ^«¢+Ù%%걃 ÅÕ½Ðí%} ½µÁ9µÅÕ½Ð줱ÐìÐìÀQ¡¸(±½°ÀÌØí%} ½µÁ9µô ½µÁÕÑÉ9µ)¹% Please inform me further. Thanks, JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)
Valik Posted October 20, 2006 Posted October 20, 2006 Do you know the name of the variable or not? If you're using a string literal with Assign(), Eval(), IsDeclared() or Call(), you've got a problem. Those 4 functions are for use when you don't know the name of the function/variable until runtime. You appear to know it at compile time. Therefore, you shouldn't be using those functions at all. As for using a global variable in a library, well, if you must use one, it shouldn't be public. Unless it's a constant. You're not exposing a constant, though. Having the user change a global variable should not be how you are handling the situation. Define a private global variable (Give it a mangled name to prevent conflicts), initialize it to the default value and then provide getter/setter functions to access it. Global $__g_CompInfoComputerName = @ComputerName Func _CompInfoComputerNameSet($sName) If $sName Then $__g_CompInfoComputerName = $sName Else $__g_CompInfoComputerName = @ComputerName EndIf EndFunc Func _CompInfoComputerNameGet() Return $__g_CompInfoComputerName EndFunc Now, any relevant code that needs to do a remote call uses the private variable $__g_CompInfoComputerName which contains the name of the computer to access. By default, it's set to @ComputerName. The user must explicitly invoke a function to override that. It can also be restored to default by passing an empty string. Much cleaner all around. Remember children, if you find yourself blatantly ignoring a guideline mentioned in the documentation, chances are you've got yourself a poor design.
JSThePatriot Posted October 20, 2006 Posted October 20, 2006 Do you know the name of the variable or not? If you're using a string literal with Assign(), Eval(), IsDeclared() or Call(), you've got a problem. Those 4 functions are for use when you don't know the name of the function/variable until runtime. You appear to know it at compile time. Therefore, you shouldn't be using those functions at all. As for using a global variable in a library, well, if you must use one, it shouldn't be public. Unless it's a constant. You're not exposing a constant, though. Having the user change a global variable should not be how you are handling the situation. Define a private global variable (Give it a mangled name to prevent conflicts), initialize it to the default value and then provide getter/setter functions to access it. Global $__g_CompInfoComputerName = @ComputerName Func _CompInfoComputerNameSet($sName) If $sName Then $__g_CompInfoComputerName = $sName Else $__g_CompInfoComputerName = @ComputerName EndIf EndFunc Func _CompInfoComputerNameGet() Return $__g_CompInfoComputerName EndFunc Now, any relevant code that needs to do a remote call uses the private variable $__g_CompInfoComputerName which contains the name of the computer to access. By default, it's set to @ComputerName. The user must explicitly invoke a function to override that. It can also be restored to default by passing an empty string. Much cleaner all around. Remember children, if you find yourself blatantly ignoring a guideline mentioned in the documentation, chances are you've got yourself a poor design.I appreciate the critique. This is how I write classes in C++, so it makes total sense. As far as the poor design goes, I suppose I should have just asked instead of automatically creating a work-around (poorly designed though it was). Thanks again Valik, JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)
sulfurious Posted October 20, 2006 Author Posted October 20, 2006 There has been a lot of talk in this thread about the downside of using Global variables, especially using OnEvent mode. Can someone explain why you would want to limit your Globals? I understand that a variable in a function is local, and that it is made and then destroyed after completion of the function. Supposed to keep resources down. But I also thought that in AutoIt, once you declared a variable, it is never actually destroyed until the script ends. I feel like I am missing something meaty here. Can anyone expound on why the preference to Local vs Global? Thanks Sul
Valik Posted October 20, 2006 Posted October 20, 2006 Name clashes. If you use a global variable named "Foo" and you use a library using a global variable named "Foo", you get to refactor your code not to use "Foo". Either that or you leave serious bugs in your application.
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