Jump to content
Sign in to follow this  
kjactive

Variable should be local but it's not

Recommended Posts

I tumbled over a thing I quite can't get right - a variable should be local but it's still accessable in the function and as I see it don't follow rules stated in variable documentations - Why...

#cs
Documentation - 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.
#ce


dim $line = 'dada'
Local_Test()
Exit

Func Local_test()
    ConsoleWrite($line & @cr)
EndFunc

Is it me that don't get something right but should the variable $line not be local and not accessable in the function - is there a dr. watson around...

Kjactive :(

Edited by kjactive

Share this post


Link to post
Share on other sites

Local = The variable is contained inside the function

Global = The variable can be used inside/outside. Any where.

Dim = Creates a varaible were you can declare/pre-initilaze a variable. Inside the current scope.

Whenever you Declare a variable using Dim it sets its value to nothing. A blank string.

h()
MsgBox(4096, "Test", $h, 10)

Func h()
   Dim $h = "lol"
   MsgBox(4096, "Test", $h, 10)
EndFunc

In the help file.

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

In the above example the "scope" is inside the function, meaning you get the error for the message box. Because the variable doesn't exist outside the function.

Edit: Corrected typo

Edited by Burrup

qq

Share this post


Link to post
Share on other sites

Local = The variable is contained inside the function

Global = The variable can be used inside/outside. Any where.

Dim = Creates a varaible were you can declare/pre-initilaze a variable. Inside the current scope.

Whenever you Declare a variable using Dim it sets its value to nothing. A blank string.

h()
MsgBox(4096, "Test", $h, 10)

Func h()
   Dim $h = "lol"
   MsgBox(4096, "Test", $h, 10)
EndFunc()

In the help file.

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

In the above example the "scope" is inside the function, meaning you get the error for the message box. Because the variable doesn't exist outside the function.

<{POST_SNAPBACK}>

EndFunc() should be EndFunc

...but that is not why I'm posting...

In reference to:

"Whenever you Declare a variable using Dim it sets its value to nothing."

Dim $h ; that would set it to nothing

Dim $h = "lol" ; seems to set it to "lol"

Dim $h ; so, this would be a "Declare"

Dim $h = "lol" ; this would be a "Declare/Assign"

I never have understood this very well, just seeking clarification.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Yeah, sorry my bad lol.

When you declare a variable you dont have to assign it a value.

Dim $test
msgbox(0,"",$test)

$test is a blank string. But you can assign it if you need to.

Dim $test = "hello"
msgbox(0,"",$test)

I use declared variables but don't declare them in most of the large scripts I creat. On the one I'm currently working on I have 13 not being assigned.

Doing variables this way can usually save your script alot of errors. In my case any way. Especially when parts of your script depend on variables that may or may not have been assigned/created yet.

Edited by Burrup

qq

Share this post


Link to post
Share on other sites

Well I don't get this - is all variables stated in start of the script globals, documentation don't state this...

Documentation - 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. - It state otherwise as in my example the variable is declared outside the function as a local variable and not declared inside the function but the function can read the variable anyway...

dim $line = 'dada'
Local_Test()
Exit

Func Local_test()
    ConsoleWrite($line & @cr)
EndFunc

Is there a dr. Watson around to explane this in plain english...

Kjactive :(

Share this post


Link to post
Share on other sites
ANY variables declared outside a function can be used ANYWHERE in the script. If you declare a variable outside a function its Local Scope is the Global Scope.

qq

Share this post


Link to post
Share on other sites

Well this is oppsite to all other languages I know off and as I see it not what stands in documentations...

Documentation: By default when variables are declared using Dim or assigned in a function they have Local scope

Thanks for a Dr. Watson reply - Sorry to say but this documentation has to be rewritten or maybe even better I think, a rework on local / global declare...

kjactive :(

Edited by kjactive

Share this post


Link to post
Share on other sites

Technically it's best to explicitly declare them, then you would have no problems, but as long as you are careful it should be ok. If my scripts get too long, I always explicitly declare them. I actually don't know why we have dim anyway.

global $test = "foo"

test()

ConsoleWrite($test)

func test()
    
    local $test = "bar"
    
    ConsoleWrite($test)
    
EndFunc

EndFunc() should be EndFunc

...but that is not why I'm posting...

In reference to:

"Whenever you Declare a variable using Dim it sets its value to nothing."

Dim $h ; that would set it to nothing

Dim $h = "lol" ; seems to set it to "lol"

Dim $h ; so, this would be a "Declare"

Dim $h = "lol" ; this would be a "Declare/Assign"

I never have understood this very well, just seeking clarification.

Yes you are right.

Dim $h ; sets aside memory, but doesnt contain anything (except maybe null or \0)

Edited by steveR

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.

Share this post


Link to post
Share on other sites

Well this is oppsite to all other languages I know off and as I see it not what stands in documentations...

Documentation: By default when variables are declared using Dim or assigned in a function they have Local scope

Thanks for a Dr. Watson reply - Sorry to say but this documentation has to be rewritten or maybe even better I think, a rework on local / global declare...

kjactive :(

<{POST_SNAPBACK}>

Common sense should tell you that Dim'ing a variable outside any function will make it global. The local scope at the global level (Outside functions) is the global scope. What else would it be local to? I see nothing wrong with the documentation in this regard. If you have to explicitly state that using Dim at the global level will create a global object, then you must also rephrase the "Local" keyword to state that using that keyword at the global level will also create a global variable since again, we are creating something local to the global scope. IMO, this adds a lot of unnecessary complexity to the documentation since it should be common sense that anything local to the global scope is inside the global scope.

Share this post


Link to post
Share on other sites

Valik, common sence is much less common then we think. Look at what a lack of common sence is doing to legal documents. :( But I do agree with you. Local and Dim can only create variables local to a function inside the function.

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
Share on other sites

To me the help file is very clear about how variables are to be used. Perphaps a better example(s) maybe. As Valik said, I find it obvious that when a variable is used outside of a function its Local Scope is the Global Scope.


qq

Share this post


Link to post
Share on other sites

In most programming languages a global variable is not accessable in a function as all variables here are locals and not accessable from or in global area ( main ) if these are not assigned as globals somewhere in main area but it seems to me that in autoit a global variable as one declared in main area is also accessable in local area without any modifications like in my simple example starting this topics - if it's so that all variables declared in global area no matter declared as local or dim are globals and accessable in functions why then do the fuzz and provide arguments to functions other than to make it local only in this function - I think that this is confusing and oppesit what I'm used too do...

That's why I deeply read the declare documentation and it state ( I think ) otherwise as this:

Documentation - By default when variables are declared using Dim :( 'OR' assigned in a function they have Local scope...

( this I read as when I declare a dim variable in global read OR in a function they have local scope - sorry but this is what it says )

Well maybe this is to much doing about assign variables but I think documentation could be more precise when this is oppesit most other languages, maeby include some examples...

Thats for replays...

kjactive :(

Edited by kjactive

Share this post


Link to post
Share on other sites

well I wrote a rewrite if one could use it - I think that it's a little more clear explane to peoble as me, new to autoit syntax...

Frontpage code but I did not made the tags in my 'fly time' but...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
<base>
<title>Language Reference - Variables</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<link href="unsaved:///../css/default.css" rel="stylesheet" type="text/css">
</head>

<body>

<h1>Language Reference - Variables</h1>
<p>A variable is just a place to store data in memory so that it can be accessed 
  quickly.&nbsp; Think of it as a mailbox in memory that you can put information 
  in or take information out of.&nbsp; For example you might create a variable 
  to store the number a user's response to a question, or the result to a math 
  equation.</p>
<p>Each variable has a name (again, similar to a mailbox) and must start with 
  the <strong>$</strong> character and may only contain <strong>letters</strong>,
<strong>numbers</strong> and the underscore <strong>_</strong> character.&nbsp; 
  Here are some example names:</p>
<p>&nbsp;&nbsp;&nbsp; $var1</p>
<p>&nbsp;&nbsp;&nbsp; $my_variable</p>
<p></p>
<p>Each variable is stored as a <a href="unsaved:///lang_datatypes.htm">variant</a>.</p>
<p></p>
<h2>Declaring Variables</h2>
<p>Variables are declared and created with the <a href="unsaved:///../keywords/Dim.htm">Dim</a>,
<a href="unsaved:///../keywords/Dim.htm">Local</a> and <a href="unsaved:///../keywords/Dim.htm">Global</a> 
  keywords:</p>
<p class="codebox">&nbsp;&nbsp;&nbsp; Dim $var1</p>
<p>Or you can declare multiple variables at once:</p>
<p class="codebox">&nbsp;&nbsp;&nbsp; Dim $var1, $myvariable</p>
<p>And durring declare provide values at one line</p>
<p>&nbsp;&nbsp;&nbsp; Dim $Var1 = 1, $myvariable = 'some text'</p>
<p>You can also assign a variable <strong>without</strong> declaring it first, 
  but many prefer explicit declarations.</p>
<p class="codebox">&nbsp;&nbsp;&nbsp; $var1 = &quot;create and assign&quot;</p>
<p></p>
<h2>Scope</h2>
<p>A variable's scope is controlled by when and how you declare the variable.&nbsp; 
  If you declare a variable at the start of your script, outside any functions 
  it exists in the <strong>Global</strong> scope and can be read or changed from 
  anywhere in the script even from within any functions without any
modifications.</p>
<p>If you declare a variable <em>inside</em> a <a href="unsaved:///lang_functions.htm">function</a> 
  it is in <strong>Local</strong> scope and can only be used <em>within that same 
  function</em>.&nbsp; Variables created inside functions are automatically destroyed 
  when the function ends.</p>
<p>By default when variables are declared in a function they have <strong>Local</strong> scope
<strong>unless</strong> 
  there is a global variable of the same name (in which case the global variable 
  is reused).&nbsp; This can be though be altered in the functions by using the <a href="unsaved:///../keywords/Dim.htm">Local</a> 
  and <a href="unsaved:///../keywords/Dim.htm">Global</a> keywords to declare variables
or to <strong>force</strong> the scope you want when returning the variables.</p>
<p></p>
<p></p>
<p></p>
<p></p>

</body>

</html>

This code is in HTML...

Maybe there is missing a note about parsing arrays - I don't know but thanks everyone for responce as I got me self a deep lektion in declare variables with autoit...

kjactive :(

Edited by kjactive

Share this post


Link to post
Share on other sites

In most programming languages a global variable is not accessable in a function

Don't you think that statement is a contradiction? How can something be "global" if its not accessible "everywhere", including inside functions? The term "global" in a programming context means the variable can be seen and used by everything. I don't know what language(s) you refer to as "most" but none that I can think of have the behavior you describe. Either you've used a language with weird, atypical behavior, or you've failed to understand how virtually all languages with scoped variables work. AutoIt's behavior is common, logical and the helpfile is clear on the matter (Assuming you do not have incorrect assumptions going in, which can't be corrected by us).

Edit: Added emphasis to some words.

Edited by Valik

Share this post


Link to post
Share on other sites

I don't know what language(s) you refer to as "most" but none that I can think of have the behavior you describe.  Either you've used a language with weird, atypical behavior, or you've failed to understand how virtually all languages with scoped variables work. 

While I understand you're explanation, I have to admit that I had taken the same sence of the scopes as Kjactive. The programming language I'm thinking of is Basic or at least QuickBasic.

In that language, the main program exists outside of all functions, but behaves like a seperate function. All variables used in the main program are inaccessible to the functions unless they are declared global.

AutoIT structurally resembles the QuickBasic way of doing things, so I kinda assumed that variables worked the same way. But, like I said - I understand your explanation and why AutoIT behaves the way it does.

Share this post


Link to post
Share on other sites

I checked out this thread, and I didn't see this, so I made an example showing why you would use Dim in a func.

The way you declare the variables in the "main" part of the script makes them all globally accessible to functions. But Dim in a function will use the global. Atleast this is what I think the point of Dim is besides using it for arrays. But needing to do this seems so rare that I feel people use Dim when they should be using Local, but thats jut my opinion.

#cs
vi:ts=4 sw=4:
#ce
#notrayicon
Opt("GUICloseOnESC",0)

Local   $test_local     = "LOCAL"
Dim     $test_dim       = "DIM"
Global  $test_global    = "GLOBAL"

local_func()
MsgBox(0,"After local_func",$test_local & @CRLF & $test_dim & @CRLF & $test_global,5)
dim_func()
MsgBox(0,"After dim_func",$test_local & @CRLF & $test_dim & @CRLF & $test_global,5)

Func local_func()
    Local $test_local, $test_dim, $test_global

    $test_local     &= "local_func"; This Functions $test_local = "local_func"
    $test_dim       &= "local_func"
    $test_global    &= "local_func"
    MsgBox(0,"local_func",$test_local & @CRLF & $test_dim & @CRLF & $test_global,5)
EndFunc

Func dim_func()
    Dim $test_local, $test_dim, $test_global

    $test_local     &= "dim_func"; Global $test_local &= "dim_func"
    $test_dim       &= "dim_func"
    $test_global    &= "dim_func"
    MsgBox(0,"dim_func",$test_local & @CRLF & $test_dim & @CRLF & $test_global,5)
EndFunc

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs

Share this post


Link to post
Share on other sites

Well all these posts just seems to point one thing out - there is a defuse information and knowhow about this vital point to autoit just as I felt previous and got confused, Me as an old programmer - I know ( as all responces pointed out ) what we are talking about when we declare some menory into a variable now ( thanks ) are all globals almost 'no matter' but as these posts stands for what it is - there is a confusion about the declare to variables in autoit and that could be why a script is that hard to debug and overview when it get longer and has to be debugged or maintainted - at least I think - My point of view that this has to be more precise documentation or maybe simplified in the language - sorry

Just ask your self - Why can a user declare Local variables in the global area and they do not get local at all, as the same goes for a dim so to speak - simplify is the keyword in autoit but not to declare variables...

It would be nice if globals was just globals no matter ( almost as they are )....

That Dim's was just locals no matter ( constants and could not be moved out of this scope )...

and Locals was just locals no matter ( do not matter where delared )...

Thats my opinion - Sorry...

kjactive :(

Edited by kjactive

Share this post


Link to post
Share on other sites

It would be nice if globals was just globals no matter ( almost as they are )....

That Dim's was just locals no matter ( constants and could not be moved out of this scope )...

and Locals was just locals no matter ( do not matter where delared )...

Thats my opinion - Sorry...

kjactive  :(

<{POST_SNAPBACK}>

Local variables are intended to be local to a function. To do that, they are declared Local inside the function they apply to. Using Local outside of all functions does not not give it a scope to apply to. :( So the current behaviour has been to put thge variable in the global list, rather than error out, not having an applicable scope.

In many versions of BASIC all variables are global, with not local variables, no matter where decleared. In QBasic and VB, (as well as many other languages, like C, C++, Java, Fortran, etc.) variables declared in a function are automatically local to the function. To declare a variable with global scope, declare it outside of the functions.

AutoIt gives the added ability to declare global variables inside a function (Global) and to reuse a global variable if it exists (Dim). I think these are good additions to the language.


David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...