Jump to content

Simple question related to GUI's and Dim


Recommended Posts

I am working on converting some code from GUIGetMsg to OnEventMode. Each GUI item is as follows...

Dim $someVariable = GUICtrlCreateSomeControl("control stuff", -1, -1, -1, -1)

Should that have the Dim, or not? I am at a loss on whether or not this is a good practice. I know that with the GUI variables you dont have to declare them at the top of the script because you are declaring and assigning at the same time. I am just curious as to whether or not it is necissary, or a good thing to have Dim before each one of the controls are created?

Thanks to everyone who replies,

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)

Link to comment
Share on other sites

In your example, to the best of my knowledge, the Dim is not necessary and the effect would not be changed if it were removed (implicit declaration follows the same scoping rules). Personally, the only thing I've ever used a plain old Dim for is when I need to create a large array but I don't want to initialize it until later.

Of course, if you dislike implicit declarations and set MustDeclareVars in your scripts, then the Dim will always be necessary even if you initialize the variable on the same line. Good practice for some other languages where variables must always be declared whether you like it or not. :P

Edited by Sokko
Link to comment
Share on other sites

HI,

I would say if you needn't to do something with the control afterwards,you do not have to waste a $avr for it.

Or do you just mean the word DIM or GLOBAL?

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

HI,

I would say if you needn't to do something with the control afterwards,you do not have to waste a $avr for it.

Or do you just mean the word DIM or GLOBAL?

So long,

Mega

I do need the variables...(Using OnEventMode) I may be able to remove them from the Labels, and other static items, and that will help some, but I am mostly curious as to whether it is a good practice to do either of the following 3 things...

  • Dim on the same line as the declaration
  • Dim or Global at the top of the script and then use it on the control
  • Dont Dim or Global, just define the variable.
I assume that it will be one of the first two options. Then the choice between those I assume again would be personal preference.

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)

Link to comment
Share on other sites

normally i use "Dim" at the top, only for those $Variables that are used inside functions on event

also you can use "Local" and use "ByRef" when sent to the function ( only on GUIGetMsg() )

but, i also would like to hear from a pro or dev

good question

8)

Thanks Valuater. That is my normal process. Dim or Global at the top (equals the same thing at the top of a script) when I need to use it in a function later (such as OnEvent), but I have never really Dim'd them at the time of initialization. Just at the top or not at all. I would love a professional opinion on it.

Thanks for your response,

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)

Link to comment
Share on other sites

If your using Dim or Local outside of functions, then your just misusing the keywords. Enough is in the helpfile to help understand scope of the keywords.

As in example.

;~ Global $var1 = True

_FuncOne()

Func _FuncOne()
    Dim $var1
    If $var1 Then
        MsgBox(0, '$var is True', $var1)
    EndIf
    MsgBox(0, 'Value of $var1', $var1)
EndFunc

If I removed the Dim $var1, then the script would crash. I do not want to give it local scope initially as it would be useless if the variable was possibly used at Global scope. If I choose to comment the Global declaration or remove it all together, then the function will continue to operate as I have designed it to do this using the Dim $var1.

Dim does indeed have purpose and if you use it outside of it's designed purpose, then your just creating a illogical script.

Link to comment
Share on other sites

  • Moderators

I was under the impression there was no need for Dim since whe have "Local" and "Global"... Dim really served no purpose (Wouldn't there would be a Dim Const if it did :P ? )

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I was under the impression there was no need for Dim since whe have "Local" and "Global"... Dim really served no purpose (Wouldn't there would be a Dim Const if it did :nuke: ? )

Global or Local will not replace the behaviour that Dim does. You would need to do a workaround to get the operation of Dim.

;~ Global $var1 = True

_FuncOne()

Func _FuncOne()
    If Not IsDeclared('var1') Then
        Assign('var1', '')
    EndIf
    If $var1 Then
        MsgBox(0, '$var is True', $var1)
    EndIf
    MsgBox(0, 'Value of $var1', $var1)
EndFunc

That looks like a 3 legged dog compared to using Dim. Au3Check does bark at your script but in comparison, using Dim is the efficient and recognizable solution. Ofcourse I could replace Assign() with Local $var1, but that is regarded as poor design :P . So, stating Dim has no purpose is like cutting off your leg :) .

Just for those who consider Global and Local as the only needed option, then a closed view is only being used. And people who misuse Dim do not have an opinion worth listening to anyway. So, IMHO, the language is taking a step backwards if Dim was removed.

Link to comment
Share on other sites

  • Moderators

Your example and explination makes sense... I had read a thread that Valik posted in, and it looks I may have taken it out of context... but since then, I've eradicated all use of it... seems to me that I've done myself an in-justice.

Here is the thread I took out of context...

http://www.autoitscript.com/forum/index.ph...st&p=112491

Here is another explination that also made it a bit clearer to me.

http://www.autoitscript.com/forum/index.ph...ost&p=78601

I'm not caught in a snare, so no need to cut off any of my limbs.

Weird how I've never had the need for it, or felt I needed something more in that aspect, since I've stopped using it all together.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I've eradicated all use of it... seems to me that I've done myself an in-justice.

Or you did not need in those scripts to use Dim for the purpose it was designed for. I would not expect most scripts to use it as it is only needed when your script is unsure at time of write that the variable will be declared and passed to the function.

I have scripts that use Dim. In reference, my shell extension application creates installation scripts As a result of a dynamically created script, some functions will be added to the script conditionally. Some variables are needed to used across more then 1 function. The variables may not be added Globally as the option for use may not be available or removed as the need is not wanted by the user. This would break existing functions that used the variables without Dimming them first. And yes, the installation scripts even use Call() as a direct function call was marked as a missing function.

I can go along with Valik with the frustration of seeing people use Dim as a Global Declaration outside of functions. Some even use Local outside of functions. It is the same with some using Call() by default, rather then directly using function calls. But the keywords/functions do have purpose that should not be misused. My opinion is different about removal of them and I would need valid reason to understand the action of doing it.

Link to comment
Share on other sites

The only situation I can recall where Dim is useful is the following: Imagine you need to turn an existing variable into an array (Such as a function parameter:

Func CreateTheArray(ByRef $aData)
    Dim $aData[50]
    ; Fill the array
EndFunc

Declaring new variables using Dim is a very bad idea because of the potential for inadvertently re-using a global variable instead of creating a new local variable.

I've revised my opinion on Dim slightly. It should only be allowed on existing variables, much like ReDim can only be used on existing arrays. It should not be possible to declare new variables with Dim.

In short, Dim should only be allowed to turn an existing variable into an array.

Link to comment
Share on other sites

Okay I do believe I have gleaned my answer out of this thread, even though no one seems to have understood exactly what I have been asking.

I am pleased to know the proper use of Dim. I never thought it should be removed from AutoIt, but I wasnt sure of all of its uses. MHz and Valik have shown a couple of uses that I have never thought of before.

I will Globally declare the variables at the top for the GUI creation. There is no other way in my opinion to keep it all organized. Having a Global or Dim throughout the code as you go through to me isnt clean, if someone else has a different opinion then please share.

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)

Link to comment
Share on other sites

Okay I do believe I have gleaned my answer out of this thread, even though no one seems to have understood exactly what I have been asking.

To sum up your topic questions.

1. You ask if you should Dim at the top of your script. My answer is you should not use Dim at the top of your script as it defeats it's purpose. Use Global.

2. You ask if you should Dim while assigning variables to controls. This is common practice in languages I have used where a declarator can also be used with assigning variables. It is personal preference where to declare, but on a script that uses MustDeclareVars, then you would be best to keep Declared variables separate (usually not assigned) so you can keep track like all Globals at top of script by default. If your not using MustDeclareVars, then you do not have to declare any variable except when an array is involved as simply assigning variables can make a script functional. Again Dim should not be used for declaring and assigning your Gui control creation as it is illogical to have those variables used already outside the function in Global use so it is a pointless declaration (use Global or Local if suitable instead).

The best answer depends on if your using "MustDelcareVars" and what your script is designed to do. AutoIt's language has been made to be flexible to be strict or just easy going depending on the user. There is no fixed method that anyone needs to follow unless it is stated to be otherwise.

Link to comment
Share on other sites

To sum up your topic questions.

1. You ask if you should Dim at the top of your script. My answer is you should not use Dim at the top of your script as it defeats it's purpose. Use Global.

2. You ask if you should Dim while assigning variables to controls. This is common practice in languages I have used where a declarator can also be used with assigning variables. It is personal preference where to declare, but on a script that uses MustDeclareVars, then you would be best to keep Declared variables separate (usually not assigned) so you can keep track like all Globals at top of script by default. If your not using MustDeclareVars, then you do not have to declare any variable except when an array is involved as simply assigning variables can make a script functional. Again Dim should not be used for declaring and assigning your Gui control creation as it is illogical to have those variables used already outside the function in Global use so it is a pointless declaration (use Global or Local if suitable instead).

The best answer depends on if your using "MustDelcareVars" and what your script is designed to do. AutoIt's language has been made to be flexible to be strict or just easy going depending on the user. There is no fixed method that anyone needs to follow unless it is stated to be otherwise.

Okay well put. Then that is what I have gleaned from the topic, but I was going on my old understanding of Dim at the initial start of the topic, and as the topic progressed it changed so it seemed that no one directly answered me.

I have decided to declare all variables (even GUI variables) at the top with Global.

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)

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...