Jump to content

Dim?


Recommended Posts

Sorry for the n00b question

I have a script with a ton of functions

one of the functions reads an INI file

$read1 = IniRead("C:\speed.ini", "settings", "vacatstart", "NotFound")

$read2 = IniRead("C:\speed.ini", "settings", "runatstart", "NotFound")

etc.. etc..etc..

but it seems as if i want to use the results of $read1 and $read2 (etc etc) in another function

I have to do a Dim $read1,$read2

at the top of the script

if i dont and i called $read1 in another function, i get the "Variable used without being declared" error

I have other issues like this throughout my script

I have a function that does a dirgetsize and does some math on the results..

in my gui under another function i get that declared error again unless i Dim that variable at the start..

so i'm sure this is a mis-understanding of the process on my part..

can someone explain what i'm not seeing?

or do i have to Dim every single variable i create? (i have alot of them)

thanks!

Link to comment
Share on other sites

As a programming practice it is always best to declare (Dim in this case) your variables. It allows the program to give the variables scope. Variable scope can be Local, Global, and just plain Dim in a scope. Different scopes are whether the variable is used in a function, or in the whole script, or included from another script and so on. Globals can be used anywhere. Locals can only be used in the scope that they were created and so forth.

As the helpfile puts it.

A variable's scope is controlled by when and how you declare the variable.  If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.

If you declare a variable inside a function it is in Local scope and can only be used within that same function.  Variables created inside functions are automatically destroyed when the function ends.

By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused).  This can be altered by using the Local and Global keywords to declare variables and force the scope you want.

There is a full page about all of this in the helpfile. Please just type in the search Variables and it will pull up a list of pages. Goto the second page. Read up and if you have any questions let me know.

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 did read the help file.. but for someone who isnt a programmer it isnt always easy to understand everything it's saying.

I had no problems becoming an advanced autoIT 2 user..

but autoIT 3 is more like a programming language.. and i stayed away from it for a long time.. so things like Dim and scopes are foriegn to me and not easily understood by noobs..

I'm trying though :)

so what i dont get is this.

I have a function, that reads a control (check box)

if it's checked, it does 1 thing and if it's not does another

here i'll post some code so i dont have to explain it.

Func cthumbs()
$fccthumbs = GUICtrlRead($Checkbox_15)
   If $fccthumbs = 1 Then
    $thmbf = DirGetSize("c:\data\thumbnails", 2)
    $thmbfr = $thmbf / 1024 / 1024
          $thmbfs = round($thmbfr,2)
    $Label_58 = GUICtrlCreateLabel($thmbfs & " mb", 200, 180, 70, 20,$SS_SUNKEN)
   EndIf
   If $fccthumbs = 4 Then
     $Label_58 = GUICtrlCreateLabel("", 200, 180, 70, 20)
   EndIf
EndFunc

it will make me Dim $Label_58 but not the rest of the var's in this code.. i dont understand why.

thanks for the assistance

Link to comment
Share on other sites

That is odd. I dont know why it would make you only dim one of those variables. The only thing I can figure is you are trying to update $Label_58 after that function is called or in another function. That would be exceeding the function scope. One other thing that just popped into mind you really dont need $Label_58 unless you are going to use it outside that function to update the label, that I would be is why it is wanting you to declare the variable with the dim statement.

What I would recommend is that you just dim all your variables from now on. You can go back if you want and dim your current ones, but that is totally up to you.

I am sorry the helpfile isnt too easily understood. I can see how to a non programmer that variable scope isnt an easy subject to grasp.

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

Thanks for the reply..

You are right.. i am using $label_58 elsewhere..

i have another function (clear) that when i click the "clear" button it basically resets all my gui stuff back to defaults.

the way i approached it was just to tied the "clear" button to a function which went back and unchecked all the check boxes and reset all the combo boxes..

i wasnt able to figure out a way to do a control set data type thing with a label (to erase it) and i think i had an issue with guidelete to..

so i ended up just doing

"$Label_58 = GUICtrlCreateLabel("", 200, 180, 70, 20)" (like if it was unchecked)

so yes i'm using label_58 elsewhere..

I will just start dim-ing my var's then..

i think that's kinda dumb that i have to.. but that's probably just my lack of understanding..

it's not so much that the help file is hard to understand.. it's just that some things are explained, while others are just assumed.. and that kinda makes it hard to pick up just from the help file..

i enjoy A3 and i enjoy learning it.. but sometimes the help file isnt enough..

again thanks for the information.

Edited by blitzkrg
Link to comment
Share on other sites

Thanks for the reply..

You are right.. i am using $label_58 elsewhere..

i have another function (clear) that when i click the "clear" button it basically resets all my gui stuff back to defaults.

the way i approached it was just to tied the "clear" button to a function which went back and unchecked all the check boxes and reset all the combo boxes..

i wasnt able to figure out a way to do a control set data type thing with a label (to erase it) and i think i had an issue with guidelete to..

so i ended up just doing

"$Label_58 = GUICtrlCreateLabel("", 200, 180, 70, 20)" (like if it was unchecked)

so yes i'm using label_58 elsewhere..

I will just start dim-ing my var's then..

i think that's kinda dumb that i have to.. but that's probably just my lack of understanding..

it's not so much that the help file is hard to understand.. it's just that some things are explained, while others are just assumed.. and that kinda makes it hard to pick up just from the help file..

i enjoy A3 and i enjoy learning it.. but sometimes the help file isnt enough..

again thanks for the information.

<{POST_SNAPBACK}>

Not a problem. I am sure in time you will understand why you should/have to dim your variables. The only reason why you do have to is if it is going to be used out side of the function it was created in. Otherwise it would have been fine.

Keep learning, if you have any more questions please ask we are certainly willing to help.

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

  • 4 weeks later...

Thanks for the reply..

You are right.. i am using $label_58 elsewhere..

i have another function (clear) that when i click the "clear" button it basically resets all my gui stuff back to defaults.

the way i approached it was just to tied the "clear" button to a function which went back and unchecked all the check boxes and reset all the combo boxes..

i wasnt able to figure out a way to do a control set data type thing with a label (to erase it) and i think i had an issue with guidelete to..

so i ended up just doing

"$Label_58 = GUICtrlCreateLabel("", 200, 180, 70, 20)" (like if it was unchecked)

so yes i'm using label_58 elsewhere..

I will just start dim-ing my var's then..

i think that's kinda dumb that i have to.. but that's probably just my lack of understanding..

it's not so much that the help file is hard to understand.. it's just that some things are explained, while others are just assumed.. and that kinda makes it hard to pick up just from the help file..

i enjoy A3 and i enjoy learning it.. but sometimes the help file isnt enough..

again thanks for the information.

<{POST_SNAPBACK}>

Could you post the code you used to reset all of your stuff? I have just recently run into needing it, and remembered this post so before I go off and reinvent the wheel I thought I would ask to take a peek at your Clear function. I am currently doing it the dirty way... deleting the GUI and restarting the application. (Not fun).

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

it's not so much that the help file is hard to understand.. it's just that some things are explained, while others are just assumed.. and that kinda makes it hard to pick up just from the help file..

i enjoy A3 and i enjoy learning it.. but sometimes the help file isnt enough..

again thanks for the information.

<{POST_SNAPBACK}>

yeah, you're going to hit snags, as awesome as the help file really is, even experienced users have to re-read some stuff sometimes. dont' get discouraged and don't feel bad about asking questions as long as you've tried to find your own answers first
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...