Jump to content

Clear Static Array


Recommended Posts

How do we wipe a static array without resizing the elements? I've gotten errors each time I've tried.

Background: I'm attempting an error stack because I'm lazier than water... I don't want to make all my error numbers unique. I also realized that I needed to resize the array to 0 since I'm returning the whole stack, but now I'm curious if this is possible.

Func __ErrorStack($iError = 0, $sFunction = "")
    
    ; The static error stack
    Static $aErrors[0]
    
    ; If the user wants the stack
    If $iError = 0 Then
        ; Copy the errors
        Local $aRet = $aErrors
        ; Clear the error stack
        <Keyword> $aErrors[1]       ;<--- Error line
        ; Return the error stack
        Return $aRet
    
    ; Otherwise, I'm adding an error
    Else
        ; Resize
        ReDim $aErrors[UBound($aErrors) + 1]
        ; And add
        $aErrors[UBound($aErrors) - 1] = $sFunction & ":" & $iError
    EndIf

EndFunc

I wasn't sure, so I tested.

; I really expected Static to work... the variable already is static... why is it trying to make it static again?
Static $aErrors[1]  ; Cannot make existing variables static.
ReDim  $aErrors[1]  ; Works... but doesn't clear the first element
Local  $aErrors[1]  ; Cannot make static variables into regular variables.
Dim    $aErrors[1]  ; Cannot make static variables into regular variables.
; These repeat their respective errors above
Static $aErrors
Local  $aErrors
Dim    $aErrors

Is this possible with static arrays?

I can think of two solutions:

  1. ReDim to 0 and ReDim back up to the original, but that seems weird. 
  2. Declare and overwrite with a second variable, again, that seems clunky
Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Link to comment
Share on other sites

Never mind. I realized I'm thinking in VBA again where using Dim a second time will create an array of the same size with empty elements. Sorry about that. I've been using Macros too much lately

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

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