Jump to content

Declared variable array, delete then re-create


Recommended Posts

Hmm. Tell me this. If I created a variable in the beginning of the script, then delete the gui it belongs to, then rebuild the gui from a function, using the same variables, but don't explicitly declare them as local, do they just inherit the scope they were orginially created with? Normally a variable created in a function is local to that function, but in this case, because guidelete did not really destroy them, they are 'remembered'?

Sul

Link to comment
Share on other sites

  • Replies 47
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

JS in my view I find that a bad pratice, prefer to make them local unless I absolutely have to and pass them byref when possible.

Also I try to make sure to use Opt("MustDeclareVars", 1) at the top of my scripts to lessen the possability of mistakes with vars.

What do you find a bad practice? (Safest to declare the variables in the scope they are needed in?) I dont think that is what you are talking about. I believe you are talking about my next sentance in which I say that I declare all my GUI variables at the top of the script as global. I dont believe I quite explained this well.

When I use OnEventMode, creating secondary GUI's I use a function so I can use my template that GUIDelete()'s. The parts of these GUI's that need to interact (Events) have to be declared outside the function. (Global Scope) Those are the only variables that I declare in the global scope.

Also I am curious as to why you would pass them ByRef...why wouldnt you just use them directly in the function.

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

What do you find a bad practice? (Safest to declare the variables in the scope they are needed in?) I dont think that is what you are talking about. I believe you are talking about my next sentance in which I say that I declare all my GUI variables at the top of the script as global. I dont believe I quite explained this well.

When I use OnEventMode, creating secondary GUI's I use a function so I can use my template that GUIDelete()'s. The parts of these GUI's that need to interact (Events) have to be declared outside the function. (Global Scope) Those are the only variables that I declare in the global scope.

Also I am curious as to why you would pass them ByRef...why wouldnt you just use them directly in the function.

Thanks,

JS

I rarely use OnEvent that pretty much forces you to use Global vars.

Byref so it's not a copy.

Just my personal preference as an old c/c++ programmer, was taught to use global as little as possible.

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

I rarely use OnEvent that pretty much forces you to use Global vars.

Byref so it's not a copy.

Just my personal preference as an old c/c++ programmer, was taught to use global as little as possible.

Yea... I am in agreeance, I dont like having to use Globals, but I have to due to the OnEventMode that I prefer :ph34r:

I was distracted when typing that last repsonse. I know ByRef isnt a copy :lmao:

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

  • Moderators

Yea... I am in agreeance, I dont like having to use Globals, but I have to due to the OnEventMode that I prefer :geek:

I was distracted when typing that last repsonse. I know ByRef isnt a copy :lmao:

JS

I'll be honest, I was so against OnEventMode when I started making GUI's because of having to make Globals, and making sure I caught everything correctly, however, I've found myself now from 99% GUIGetMsg to 90%.

Switch has become my new best friend. I have my loops and regular status all rolled into one on command :ph34r: .

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'll be honest, I was so against OnEventMode when I started making GUI's because of having to make Globals, and making sure I caught everything correctly, however, I've found myself now from 99% GUIGetMsg to 90%.

Switch has become my new best friend. I have my loops and regular status all rolled into one on command :lmao: .

Yea... I need to work out my own template for GUIGetMsg, as I have created for template mode.

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

I rarely use OnEvent that pretty much forces you to use Global vars.

Byref so it's not a copy.

Just my personal preference as an old c/c++ programmer, was taught to use global as little as possible.

Create an enumeration that provides an index to each control as well as final member which is the size of the array you'll later use:
Enum $ID_HWND, $ID_OPTIONS, $ID_SAVE, $ID_STOP, $ID_CLEAR, $ID_CHILD, $ID_LISTVIEW, $ID_MAXoÝ÷ ÙKjȧW Úªëk+h]jY¢{[Öqë,¶¦¾&¢×±jëh×6Global $g_aControls[$ID_MAX]
Link to comment
Share on other sites

Create an enumeration that provides an index to each control as well as final member which is the size of the array you'll later use:

Enum $ID_HWND, $ID_OPTIONS, $ID_SAVE, $ID_STOP, $ID_CLEAR, $ID_CHILD, $ID_LISTVIEW, $ID_MAXoÝ÷ ÙKjȧW Úªëk+h]jY¢{[Öqë,¶¦¾&¢×±jëh×6Global $g_aControls[$ID_MAX]
Umm not to sound like a total noob, but is there an example of this somewhere. I have tried this but haven't really found a great method for doing it.

The onevent vs. getmsg, can one of you give a sort of 'why' one would be preferred over the other? Having some vb experience, the onevent seems logical and easy. I have never even used the getmsg method. Am I seriously missing something that is much easier or more robust?

Thanks,

Sul

Link to comment
Share on other sites

Create an enumeration that provides an index to each control as well as final member which is the size of the array you'll later use:

Enum $ID_HWND, $ID_OPTIONS, $ID_SAVE, $ID_STOP, $ID_CLEAR, $ID_CHILD, $ID_LISTVIEW, $ID_MAXoÝ÷ ÙKjȧW Úªëk+h]jY¢{[Öqë,¶¦¾&¢×±jëh×6Global $g_aControls[$ID_MAX]
Valik I appreciate the information you have provided. It has definitely taught me some new technique's.

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

Valik, do you mean to use Enum and add incrementally all of your controls in the order they are made so that you can have an array that will reference them? Or, is it somehow letting you set your variables there so that you can reference them by name with an array call, and pull the ID from that?

I see the Enum help file, and frankly did not really understand it's use. I think I get what you are saying, I just wonder what the data is that is put in/ returned from the Enum in your reference.

Can you shed a little light perhaps, or a pointer in the right direction?

Sul

Link to comment
Share on other sites

The onevent vs. getmsg, can one of you give a sort of 'why' one would be preferred over the other? Having some vb experience, the onevent seems logical and easy. I have never even used the getmsg method. Am I seriously missing something that is much easier or more robust?

Thanks,

Sul

There are definite benefits to both methods. I would say a positive benefit for OnEventMode, is it gives precedence to your events. If you are in the middle of something (loop or other directions) the GUI can still be used. Whereas GetMsgMode when in the middle of something else leaves the GUI completely un-responsive.

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

Valik, do you mean to use Enum and add incrementally all of your controls in the order they are made so that you can have an array that will reference them? Or, is it somehow letting you set your variables there so that you can reference them by name with an array call, and pull the ID from that?

I see the Enum help file, and frankly did not really understand it's use. I think I get what you are saying, I just wonder what the data is that is put in/ returned from the Enum in your reference.

Can you shed a little light perhaps, or a pointer in the right direction?

Sul

Yes he is suggesting having an array that will control all of your Global GUI variables. The Enum is so you can easily control that array with text. You shouldnt have to remember that control 162 is a button containing "Submit". With Enum you can create a list of variables (as in his example) that will help you by remembering with words.

;Enum Values: 0, 1, 2, ... , 162, 163, 164
Enum $ID_HWND, $ID_BTN_OKAY, $ID_BTN_CANCEL, ... , $ID_BTN_SUBMIT, $ID_BTN_CLEAR, $ID_MAX
;To access that button you dont need to remember the number 162, just $ID_BTN_SUBMIT, $ID_MAX is at the end of the Enumeration to help the creation of the array, again without having to count yourself.
Global $g_aControls[$ID_MAX]

;Access $ID_BTN_SUBMIT
$g_aControls[$ID_BTN_SUBMIT]

Does that help any further?

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

I think I get it. Since Enum starts at what you set it to, and I think steps what you step by, you can effectively create, at Enum position 1, a variable named $variable. Each step next in the Enum list will apply the incremented number to the next $variable_name, thus creating the controlID's for each $variable before any (I presume) other controls are made in the script. And you are then saying that the last $variable made will be your array.

I can see one very very big advantage to that. Anytime I need to know the ID of the control on an event procedure, I already know it is, say, going to be in the first 15 ID's, and better yet, I can even reference the $array[$btnNAME] because it is in this type of statement (which is a really really quick way to do that), versus making the variables as the script is built, and having to debug a bit it you wish to identify what ControlID to watch for, like ControlID 4240 or ControlID 16 (funny how they get so big so quickly). That is definately on my next to do list.

I have been messing with a way to hold all of my buttons, labels, inputs, what have you, into an array of controls, so that I can 'For Each Button In Form.Buttons' type of thing. This may just be my answer to that.

Thank you for the explanation JS, that definately helps. And thanks Valik for bringing that up.

later,

Sul

Link to comment
Share on other sites

I think I get it. Since Enum starts at what you set it to, and I think steps what you step by, you can effectively create, at Enum position 1, a variable named $variable. Each step next in the Enum list will apply the incremented number to the next $variable_name, thus creating the controlID's for each $variable before any (I presume) other controls are made in the script. And you are then saying that the last $variable made will be your array.

I can see one very very big advantage to that. Anytime I need to know the ID of the control on an event procedure, I already know it is, say, going to be in the first 15 ID's, and better yet, I can even reference the $array[$btnNAME] because it is in this type of statement (which is a really really quick way to do that), versus making the variables as the script is built, and having to debug a bit it you wish to identify what ControlID to watch for, like ControlID 4240 or ControlID 16 (funny how they get so big so quickly). That is definately on my next to do list.

I have been messing with a way to hold all of my buttons, labels, inputs, what have you, into an array of controls, so that I can 'For Each Button In Form.Buttons' type of thing. This may just be my answer to that.

Thank you for the explanation JS, that definately helps. And thanks Valik for bringing that up.

later,

Sul

I would like to clarify something really quickly. It doesnt hold the ControlID until you do something like as follows...

;Enum Values: 0, 1, 2, ... , 162, 163, 164
Enum $ID_HWND, $ID_BTN_OKAY, $ID_BTN_CANCEL, ... , $ID_BTN_SUBMIT, $ID_BTN_CLEAR, $ID_MAX
;To access that button you dont need to remember the number 162, just $ID_BTN_SUBMIT, $ID_MAX is at the end of the Enumeration to help the creation of the array, again without having to count yourself.
Global $g_aControls[$ID_MAX]

$g_aControls[$ID_HWND] = GUICreate("New GUI", 200, 200)
$g_aControls[$ID_BTN_OKAY] = GUICtrlCreateButton("Okay", 10, 20)

;...

$g_aControls[$ID_BTN_SUBMIT] = GUICtrlCreateButton("Submit", 150, 150)

You actually dont even have to create the controls in the same order they are Enumerated due to the Array size already being declared by the $ID_MAX.

If you are going to use the Enum with an Array you will not want to use any incrementation other than the default, but you have a good grasp of what Enum can do.

I hope this helps you a bit further :lmao:

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

Enum (scope) $var1,$var2,$var3,$vMax

dim $array[$vMax]

At this point, the array holds 1,2,3,4 - using $array[$var2] references $var2 = 2, so like saying $array[2]. Then simply creating a control using $array[2] creates a ControlID of 2 for that object, thus if I were to look for ControlID = 2 in an event, I could reference $array[$var2].

Basically telling app what ID to give control, and having text to number reference for ease of use?

Would it also not be a good practice to declare $array[0] = $vMax?

I think that is it. True?

Thanks yet again.

Sul

Edited by sulfurious
Link to comment
Share on other sites

@Valik

Thanks I had something similar, but longer winded typed up when I had a power failure. :lmao:

@sulfurious

At this point, the array holds 1,2,3,4 - using $array[$var2]references $var2 = 2, so like saying $array[2]. Then simply creating a control using $array[2] creates a ControlID of 2 for that object, thus if I were to look for ControlID = 2 in an event, I could reference $array[$var2].

Basically telling app what ID to give control, and having text to number reference for ease of use?

Simply creating a control using $array[2] is the same as creating it using $array[$var2], and it doesnt assign the ControlID as anything. The ControlID doesnt become 2. That is just the number of the array element so you can access it. In an event you can access $array[$var2], as that will return the proper "ControlID".

I may have mis understood your quoted section above. If that is the case then disregard what I just said if not then I hope this sends you in the right direction.

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

Ah, Enum starts at 0. I was thinking, or wasn't, that it was at 1, and that 0 would be blank. I get used to using $array[0] in some of the functions.

So, if using an array val to = make control is the same as using an enumed array val to = make control, and both have a returned value of the ControlID, it would appear that enum, in this instance, is just for ease of use?

If that is the case, what would the use of Enum be otherwise?

I think I get what it does, but I don't quite see how it is that much different than just creating the variable name = make control and referencing it.

Has it to do with the whole scope of the variable?

Or is this a method of creating variables that is somehow advantageous over creating them elsewhere. I guess I can do the same thing, I think, with other methods, so I am struggling to understand why and when to use it. Something nags at me I could use this, so trying to grasp it.

Thanks tho, again, for the information.

Sul

Link to comment
Share on other sites

Ah, Enum starts at 0. I was thinking, or wasn't, that it was at 1, and that 0 would be blank. I get used to using $array[0] in some of the functions.

So, if using an array val to = make control is the same as using an enumed array val to = make control, and both have a returned value of the ControlID, it would appear that enum, in this instance, is just for ease of use?

If that is the case, what would the use of Enum be otherwise?

I think I get what it does, but I don't quite see how it is that much different than just creating the variable name = make control and referencing it.

Has it to do with the whole scope of the variable?

Yes the $array[0] that some functions use was created before UBound(), but I think eventually the way to go will be UBound().

Enum in this case is definitely for ease of use, and making the script readable. You could just do $array[0] = GUICreate(), $array[1] = GUICtrlCreateButton(), ... , $array[75] = GUICtrlCreateCombo() (It would be hard to remember which variable went where.

There are many uses for Enum since it can be set to any scope, and the amount of the enumeration between variables can be changed for each instance.

Creating individual variable names for say 400 controls would be hard to keep up with, I dont know that using the array, and Enum will be terribly much easier, but in my opinion after having only used it for a short bit, and with lots of variables it is easier for me.

I find that it is more organized.

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

Ah. I use ubound a lot, even if an array uses a count in [0]. I was curious as to why some arrays used it and some did not, as returned arrays that is.

You have given a good example of the use of Enum. I was thinking it might be more complex, but does not appear to be so.

I can see that some of my static arrays will be much easier to create with Enum.

Thanks for the help.

Sul

Edited by sulfurious
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...