Jump to content

Dim / Global / Local / Const . What does it mean?


Recommended Posts

Dim is just the same as just typing out the variable

dim $var ;Same as just typing $varoÝ÷ غjU¢W^rV«y«Ú®&ëaj×åÉ·®±çb©hq©lr^iû§rبú+{¦¦WºÚ"µÍ[ÈÙ[Ý[Û
BØØ[  ÌÍÝHHØØ[ÙHÈXXH[]ÚHÝ]ÚYHÈ[Ý[Û[[oÝ÷ Ø Ý
췥תÞjö«¦åzØZµÆ§[yÈZ¶èºhºÛazÇ+AÆ®±è­À5$À56®¶­sdvÆö&Â6öç7Bb33c·f"ÒµvÆÂFV6Æ&RvÆö&Âf&&ÆRFB6ææ÷B&R6ævVBçvW&R'WB6â&RW6VBWfW'vW&

hope you understand :mellow:

Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Link to comment
Share on other sites

Dim is just the same as just typing out the variable

dim $var ;Same as just typing $varoÝ÷ غjU¢W^rV«y«Ú®&ëaj×åÉ·®±çb©hq©lr^iû§rبú+{¦¦WºÚ"µÍ[ÈÙ[Ý[Û
BØØ[  ÌÍÝHHØØ[ÙHÈXXH[]ÚHÝ]ÚYHÈ[Ý[Û[[oÝ÷ Ø Ý
췥תÞjö«¦åzØZµÆ§[yÈZ¶èºhºÛazÇ+AÆ®±è­À5$À56®¶­sdvÆö&Â6öç7Bb33c·f"ÒµvÆÂFV6Æ&RvÆö&Âf&&ÆRFB6ææ÷B&R6ævVBçvW&R'WB6â&RW6VBWfW'vW&

hope you understand :(

..... i'm not sure why when I quoted your post it turned to garbage :mellow:

Cool Thanks Marlo! I now have a couple of further questions:

1. If these are the same:

dim $var

$var

Why do people use dim and not just type $var

2. When I don't use global $var below, i can still use the fuction to change $var is my code too simple?

$var = 1

_function()
func _function()
    $var = 2
EndFunc

msgbox(0,"blah", $var)
Link to comment
Share on other sites

I think you use Dim $var if you have the $var but the script cant find it.

I had it once.

AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

What about this exmaple for Local.

Dont know if its right.

$var = 50
MsgBox(0, "", "$var = " & $var)
Func MyFunc()
    Local $var = 100
    MsgBox(0, "", "$var in MyFunc = " & $var)
EndFunc
MsgBox(0, "", "$var = " & $var)

AlmarM

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

..... i'm not sure why when I quoted your post it turned to garbage :mellow:

The autoit tags cause problems, change them to code tags to avoid the issue.

1. If these are the same:

dim $var

$var

Why do people use dim and not just type $var

They are not the same. You can't declare a variable by just "$var", but the declaration will be made for you if you assign a value to it, like: "$var = 0"

The "= 0" part that assigns a value is required if you don't want Dim/Local/Global.

2. When I don't use global $var below, i can still use the fuction to change $var is my code too simple?

$var = 1

_function()
func _function()
    $var = 2
EndFunc

msgbox(0,"blah", $var)
This works because you provided an assignment value, so $var was declared by default. Since that line is outside a function, it was declared Global by default. Dim and declaration by assignment will do the same thing, declare Global by default outside a function, or Local by default if inside a function. If the variable already exists in the Global scope however, then that is the variable used inside a function by "$var = 2", a new Local is not created by assignment because it already exists globally. To create a separate variable with the same name in the local scope, you would have to explicitly use Local inside the function:
$var = 1; Global by default assignment

_function()
msgbox(0,"Global $var outside function", $var)

func _function()
    msgbox(0,"Global $var inside function", $var)
    Local $var = 2; This is not the same $var, it is local to this function
    MsgBox(0,"Local $var inside function", $var)
EndFunc

This get confusing and leads to problems, as you can see. To avoid those problems, never use default assignment or Dim, always explicitly declare variables outside functions with Global and all variables used inside functions with Local (unless the function really wants to use the Global). Even though there are default actions that _might_ take care of it for you, explicit declaration will save you many headaches.

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

explicit declaration will save you many headaches.

:mellow:

Sorry to keep this alive, but just to clarify.

1. declaring: $var =0 or dim $var is the same.

2.$var=0 or dim $var will normally automatically assign itself to global $var

3. if no global $var exists, then using dim $var/$var=0 inside a function will automatically be the same as local $var

I've seen a few example scripts where the varibles are declared using dim at the top of the script. I assume that using global at the beginning of the script then has the same effect.

this seems like a difficult concept, I think I'm getting it though. Thanks for the pointers and great examples.

Link to comment
Share on other sites

Sorry to keep this alive, but just to clarify.

1. declaring: $var =0 or dim $var is the same.

2.$var=0 or dim $var will normally automatically assign itself to global $var

3. if no global $var exists, then using dim $var/$var=0 inside a function will automatically be the same as local $var

Right on all three counts. :mellow:

I've seen a few example scripts where the varibles are declared using dim at the top of the script. I assume that using global at the beginning of the script then has the same effect.

this seems like a difficult concept, I think I'm getting it though. Thanks for the pointers and great examples.

Using Global, Dim, and simply declaring by assignment will all produce a Global variable if done outside of a function. The only difference is good style and therefore the confusion you can generate by not being explicit. The longer and more complicated your scripts get, the more you will appreciate doing it right and being able to understand it when you go back to make a change after not looking at this code for a year. The "What was I thinking?" moment... :(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...