Jump to content

Dim / Global


charvi
 Share

Recommended Posts

Is it normal that the Dim and Global have the same effect ?

A Global variable should be used anywhere, but a Dim variable should not be recognized in functions?!

What is the difference between a Dim and a Global variable?

Dim $d1 = 1
Global $g1 = 1

MsgBox(0,"","D1="&$d1&@CR&"G1="&$g1)

dim $x
test($x)
MsgBox(0,"","D1="&$d1&@CR&"G1="&$g1)

func test($x)
MsgBox(0,"","D1="&$d1&@CR&"G1="&$g1)    
$d1 += 1
$g1 += 1
EndFunc
Link to comment
Share on other sites

@charvi

Hi,

Youve said all, Dim is for declare variable before use it or if the variable is in function;

Global is also for declare variable, but to add specific parameters; for exemple

Global $g=1;$g=1 for all script
Global $g=2;$g=2 for all script
Edited by FireFox
Link to comment
Share on other sites

@charvi

Hi,

Youve said all, Dim is for declare variable before use it or if the variable is in function;

Global is also for declare variable, but to add specific parameters; for exemple

Global $g=1;$g=1 for all script
Global $g=2;$g=2 for all script
I think that is misleading. Well, it's not correct.

For creating array variables there is no difference between Global and Dim when they are used in the body of the script outside of functions.

When used inside a function Dim will declare the variable as local so then it is different to Global.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

@FireFox, Dim can also be used for adding specific parameters, so, at that point there is no difference.

@martin, that's already a real difference, Dim inside the function is local.

I was asking what is the difference, because when the program goes in a function, I want all variable set to zero, except those declared as Global, to avoid confusion. For example, when you create a window: $hWin=GUICreate(.....), it is possible that a function creates his own window too, with the same $hWin variable, which will overwrite the initial value.

Sure, it is possible to use another variable, but when you have hundreds or thousands of functions, it is not possible to be 100% sure whether they are already used or not.

So, how are you avoiding double usage? By declaring ALL variables as LOCAL in ALL functions, is this the correct solution?

Link to comment
Share on other sites

  • Moderators

@FireFox, Dim can also be used for adding specific parameters, so, at that point there is no difference.

@martin, that's already a real difference, Dim inside the function is local.

I was asking what is the difference, because when the program goes in a function, I want all variable set to zero, except those declared as Global, to avoid confusion. For example, when you create a window: $hWin=GUICreate(.....), it is possible that a function creates his own window too, with the same $hWin variable, which will overwrite the initial value.

Sure, it is possible to use another variable, but when you have hundreds or thousands of functions, it is not possible to be 100% sure whether they are already used or not.

So, how are you avoiding double usage? By declaring ALL variables as LOCAL in ALL functions, is this the correct solution?

I personally declare all variables in functions "Local" and all of them in the body as "Global". You won't find the word "Dim" in any of my AutoIt Scripts other than "ReDim" which works on both Local and Global anyway.

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

@SmOke_N: That's a good discipline. I'm not new in programming but new in AutoIt, so I'm trying to give my scripts (and my brain!) a good discipline with the variables.

So far, I have:

- for declarations: Global declaration in the main body, Dim in the functions (thanks SmOke_N!)

- for constants: they all begin with $c

- for handles: they all begin with $h

- for the menu in the main body: they begin with $m (I will have many items)

- for numeric values: they all begin with $n

- for string values: they all begin with $s

- naming the functions: add an underscore before and after the function name, such as _getsize_(...)

If there is something else to pay attention at, please let me know. It's good to know all these small things before 'reshaping' all the variables.

When do you use the word 'LOCAL' then?

Link to comment
Share on other sites

When do you use the word 'LOCAL' then?

When you're writing functions. Like this:

Func Area($nwidth,$nHeight)
    ; This variable is only needed in this function
    ; This means that it will be created and destroyed everytime the function is called
    Local $nArea
    $nArea=$nwidth*$nHeight
    Return $nArea
EndFunc

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

  • Moderators

@SmOke_N: That's a good discipline. I'm not new in programming but new in AutoIt, so I'm trying to give my scripts (and my brain!) a good discipline with the variables.

So far, I have:

- for declarations: Global declaration in the main body, Dim in the functions (thanks SmOke_N!)

- for constants: they all begin with $c

- for handles: they all begin with $h

- for the menu in the main body: they begin with $m (I will have many items)

- for numeric values: they all begin with $n

- for string values: they all begin with $s

- naming the functions: add an underscore before and after the function name, such as _getsize_(...)

If there is something else to pay attention at, please let me know. It's good to know all these small things before 'reshaping' all the variables.

When do you use the word 'LOCAL' then?

I generally follow this format with my vars:

-constants: Whatever the constant is in MSDN ie... WM_SIZE becomes $WM_SIZE for me.

-variant: $v_

-structs: $t_

-pointers: $p_

-handles: $h_

-strings: $s_

-integer: $i_

-float/double: $n_

-array: $a_

-boolean: $f_

-byte: $b_

I think that about covers it for me.

I'm not totally in line with: http://www.autoitscript.com/autoit3/udfs/UDF_Standards.htm ... but I can read my own scripts lol...

Edit:

Because of: http://www.autoitscript.com/forum/index.ph...st&p=613528

Edited by SmOke_N

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

@SmOke_N:

-structs: $t_

Many thanks for the UDF link and your slightly different way. I have one question: what do you mean with -structs? What kind of variables is that?

@trancexx: Thanks for telling that, as I was unaware of this fact. So usage of Global and Local *only* is recommended :)

Link to comment
Share on other sites

  • Moderators

@SmOke_N:

Many thanks for the UDF link and your slightly different way. I have one question: what do you mean with -structs? What kind of variables is that?

http://msdn.microsoft.com/en-us/library/ms536136(VS.85).aspx

Example:

Global $t_rect = DllStructCreate("long left;long top;long right;long bottom")

Edit:

This reminds me, I use another definition for vars:

pointers-$p_

Example:

Global $t_rect = DllStructCreate("long left;long top;long right;long bottom")
Global $p_rect = DllStructGetPtr($t_rect)
Edited by SmOke_N

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

@monoceres: in this case, my discipline for functions is now:

- for declarations: Global or Dim declarations in the main body, Local in the functions

I believe that's the safest of all. Thanks monoceres.

It may be "safe" but it's stupid and it's not the "safest of all". The safest is to just not use Dim. And that's doubly true if you're already using Global in some places. Consistency is also important and haphazardly using Global and Dim to mean the same thing is just needlessly confusing when you could use Global and be explicit all the time.

I wish I could remove Dim from the language. And not piss off a flock of retards.

Edit: Added last sentence.

Edited by Valik
Link to comment
Share on other sites

I wish I could remove Dim from the language. And not piss off a flock of retards.

Couldn't it just become an undocumented feature, removing all traces of it in both the help file and in SciTe? That way the keyword will sooner or later be forgotten, but old script will not break. Kinda how _ArrayCreate() is still in array.au3.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

@Valik: "consistency" is a beautiful word, and that's why I choosed to learn AutoIt, as I noticed it's a serious language, easy to understand, and with excellent documentation and examples. At the beginning I was doubting because there are no directives or functions to print on paper. Luckily I found martin's PrintMG program, I came back... Hopefully his modules will be standard included in AutoIt in a next version. I was very surprised that such a good language was unable to print out on paper... But the future of AutoIt is still bright, isn't it? :)

I have already begun to replace all Dim's with Global's....

Link to comment
Share on other sites

Couldn't it just become an undocumented feature, removing all traces of it in both the help file and in SciTe? That way the keyword will sooner or later be forgotten, but old script will not break. Kinda how _ArrayCreate() is still in array.au3.

A good idea foiled by retards. I guess you don't realize how many stupid fucks think that errors reported by Au3Check* are errors reported by AutoIt. I can already see the flood of fucktard tickets that will appear on Trac because people don't understand their toolset.

*Au3Check files are generated from the documentation. While we can manually add things, that also defeats the point of deprecating a feature when people aren't beaten over the head to remove it.

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