Relive

"Cannot make existing variables static."

4 posts in this topic

#1 ·  Posted (edited)

This is the error message I got: "Cannot make existing variables static."

I'm trying to use static variable as a means to iterate a section of my script. 

Original

while 1
    global $initial, $constant
    $x = $initial + ($constant * SomeFunc())
    msgbox(0, '', $x)
wend

func SomeFunc()
    global static $counter = -1
    $counter += 1
endfunc

Edit1

while 1
    global $initial, $constant = 1
    $x = $initial + ($constant * SomeFunc())
    msgbox(0, '', $x)
wend

func SomeFunc()
    global static $counter = -1
    $counter += 1
    return $counter
endfunc

 

Edited by Relive

Share this post


Link to post
Share on other sites



You are trying to redeclare a global static variable.

Apart from that not making much sense, I expect it's not allowed.

What else makes no sense is your mathematical operations involving 3 variables all of which are 0 and a function return which is also 0.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You are trying to redeclare a global static variable.

Apart from that not making much sense, I expect it's not allowed.

What else makes no sense is your mathematical operations involving 3 variables all of which are 0 and a function return which is also 0.

So if I wrote local static variable, it works... It doesn't make any more sense than declaring global. Also, let $constant = 1, and attempt return $counter (in global please).

Edited by Relive

Share this post


Link to post
Share on other sites

A global static is pointless.

Plus, don't declare your variables inside a loop, it's a performance drain to do that, and never declare global variables inside a function.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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

  • Similar Content

    • guinness
      By guinness
      An example (or one variation at least) that I created in a discussion we (devs/mods) were having about protecting global variables.
      ; An enumeration for the getters and setters, as I hate having to write zero and one Global Enum $VERSION_WRAPPER_GET, $VERSION_WRAPPER_SET ; Unused, as I have hidden behind functions, so users who might feel the need to do this $g_iVersion = 'Haha, I''m no a number', can't! ; Global $g_iVersion = 0 ; Basically this is encapsulation and is just one of many principals found in Object Oriented Programming (OOP) ; NOTE: There is no way for me to access the "global" version variable without using the _Get/Set_Version() function(s) Example() Func Example() ; Set to 10, as only integer datatypes are allowed _Set_Version(10) ; Displays 10 ConsoleWrite('1. ' & _Get_Version() & @CRLF) ; Set to a non-digit. Such a bad user/dev! _Set_Version('Not a number') ; Displays 10, as a string was passed previously that wasn't valid ConsoleWrite('2. ' & _Get_Version() & @CRLF) ; Call another function to show that it's not just visible within this function Another_Func() ; Displays 99, which was previously set in the Another_Func() ConsoleWrite('4. ' & _Get_Version() & @CRLF) ; If the variable located in __Version_Wrapper() was purely a global variable, then anything could be assigned to it ; without proper checking. As you can see below, the variable is only assigned to when certain criteria is met ; Before someone mentions performance, you have to weigh up the pros and cons of such a design choice. If you really need data to be protected ; from your users mingling with it, then this is one of many correct approaches to encapsulate data. Otherwise if you're not concerened, then ; maybe it's not for you. You're the developer when it comes down to it EndFunc ;==>Example Func Another_Func() ; Displays 10 ConsoleWrite('3. ' & _Get_Version() & @CRLF) ; Set to 99 and return back to the previous function i.e. Example() _Set_Version(99) EndFunc ;==>Another_Func ; Getter Func _Get_Version() Return __Version_Wrapper($VERSION_WRAPPER_GET) EndFunc ;==>_Get_Version ; Setter Func _Set_Version($iVersion) Return __Version_Wrapper($VERSION_WRAPPER_SET, $iVersion) EndFunc ;==>_Set_Version ; This is a wrapper for the local static variable that will be visible only in the function and for the duration of the application's ; lifecycle ; $iType: Either $VERSION_WRAPPER_GET or $VERSION_WRAPPER_SET ; $iVersion: Used when the type is $VERSION_WRAPPER_SET Func __Version_Wrapper($iType, $iVersion = Null) ; Create a local static variable and initialise to 0 Local Static $s_iVersion = 0 Switch $iType Case $VERSION_WRAPPER_GET ; Getter Return $s_iVersion Case $VERSION_WRAPPER_SET ; Setter ; Now is the chance to check if the value meets certain criteria e.g. [0-9]+ and not a string datatype If StringRegExp($iVersion, '^\d+$') And Not IsString($iVersion) Then $s_iVersion = $iVersion EndIf EndSwitch ; Return null by default, especially if the type is as a setter Return Null EndFunc ;==>__Version_Wrapper  
    • Apples292
      By Apples292
      Hi everyone,
      Using this script (it has a broken link but here are the files) I'm trying to record a short clip 1 second or shorter longer (although you can assume it's always 1 second) and every time I record the clip (mp3 however it's the same on wav) it ends up with an extremely short static sound at the start of each clip even with the highest quality sound. Is there anyway you can trim the file down, also using bass, for example taking the first 100 miliseconds off the start (it has to be mp3 though)? Or on the other hand, does anyone know of another bass recording tool that allows that diversity of options, e.g quality (not essential but preferable), volume and microphone choices (these two are important) which doesn't have this issue?
      Thanks
      Apples292
    • MvGulik
      By MvGulik
      Playing around a bit with this idea.

      Note: Needs at least AutoIt version 3.3.9.0 (#1257: Fix bugs in Static array handling)


      Func My_Statics($sName, $vData = '') ;; parameter checkups If 1 Then ;; ... If Not IsString($sName) Then Return SetError(2) ;; name -> string only. If Not $sName Then Return SetError(3) ;; name -> no empty string. ;If StringInStr($sName, ' ') then Return SetError(4) ;; block spaces in names. ... etc ;; not checking for additional odd name cases. (presuming general valid variable name cases.) ;; note: name string are not case-sensitive in this case. EndIf Local Enum $_ind_ ;; array index field. (make global when warranted.) Local Static $asNames[1] = [0] Local Static $avData[1] = [0] Local $iSize = $asNames[$_ind_] Local $out = '' Local $err = 0 If @NumParams > 1 Then ;; data input mode. Do ;; used as name-found bailout point. For $i = 1 To $iSize If $asNames[$i] = $sName Then $avData[$i] = $vData ExitLoop 2 EndIf Next ;; Name not found. Add new name entry. $iSize += 1 ReDim $asNames[1 + $iSize] ReDim $avData[1 + $iSize] ; $asNames[$_ind_] = $iSize $avData[$_ind_] = $iSize ; $asNames[$iSize] = $sName $avData[$iSize] = $vData Until 1 Else ;; recall $err = 1 For $i = 1 To $asNames[$_ind_] If $asNames[$i] = $sName Then $out = $avData[$i] $err = 0 ExitLoop EndIf Next EndIf Return SetError($err, 0, $out) EndFunc
      (Only tested with strings and a array. But seems ok so far.)
      Error 1 (+empty string): Name not found, recall mode only.
      Error 2+3: Name parameter hickup.

      - Fixed missing constant.