Jump to content

If I have a Const variable, can I delete it?


Recommended Posts

I declared a const array, that I won't change and I can not change in the entire program. (Safety)

Exept one point, one point I need to change this variable.

My question is that can I delete the const variable and redeclare it?

Or I need to remove Const to change the variable.

If I remove const how can I make my variable "safe", because this variable can not be changed only the declaration and one point in the whole program.

Thanks in advance!

Link to post
Share on other sites

You cannot redefine/delete a Const within the same scope.  But you can if you change the scope too.

#include <Array.au3>

Global Const $aConst[3] = [1,2,3]
_ArrayDisplay($aConst)

DoIt()

Func DoIt()
  Local $aConst[4] = [4,5,6,7]
  _ArrayDisplay($aConst)
EndFunc

This is one way to override the previous declaration of a variable.

Link to post
Share on other sites

Setting a variable/Array to Const is kind of a security measure to prevent unintended modification.
Remove Const and do the "security" checking yourself.

When you are ready to put your script into production scan the code for the name of the array and make sure that none of the found lines changes the array.
Have a careful look at parameters as the name of the array parameter might change.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites
#AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

#AutoIt3Wrapper_UseX64=Y

Opt( "MustDeclareVars", 1 )

Example()

Func Example()
  Local $oDict = ObjCreate( "Scripting.Dictionary" )
  $oDict.Item( "Int" ) = 1
  $oDict.Item( "Flt" ) = 1.1
  $oDict.Item( "Str" ) = "One"
  Local Const $aArray[1] = [ $oDict ] ; Local Const
  $oDict = 0
  ConsoleWrite( "$aArray[0].Item( ""Int"" ) = " & $aArray[0].Item( "Int" ) & @CRLF )
  ConsoleWrite( "$aArray[0].Item( ""Flt"" ) = " & $aArray[0].Item( "Flt" ) & @CRLF )
  ConsoleWrite( "$aArray[0].Item( ""Str"" ) = " & $aArray[0].Item( "Str" ) & @CRLF & @CRLF )

  Local $oMyDict = $aArray[0]
  $oMyDict.Item( "Int" ) = 2
  $oMyDict.Item( "Flt" ) = 2.2
  $oMyDict.Item( "Str" ) = "Two"
  $oMyDict = 0
  ConsoleWrite( "$aArray[0].Item( ""Int"" ) = " & $aArray[0].Item( "Int" ) & @CRLF )
  ConsoleWrite( "$aArray[0].Item( ""Flt"" ) = " & $aArray[0].Item( "Flt" ) & @CRLF )
  ConsoleWrite( "$aArray[0].Item( ""Str"" ) = " & $aArray[0].Item( "Str" ) & @CRLF & @CRLF )
EndFunc

Console output:

$aArray[0].Item( "Int" ) = 1
$aArray[0].Item( "Flt" ) = 1.1
$aArray[0].Item( "Str" ) = One

$aArray[0].Item( "Int" ) = 2
$aArray[0].Item( "Flt" ) = 2.2
$aArray[0].Item( "Str" ) = Two

 

Link to post
Share on other sites
5 hours ago, DannyJ said:

how can I make my variable "safe"

Well, you're the coder. Declaring a value, is a value. If you where, in say C, the Const goes in a different memory space than a variable but, you're in AutoIt. Here it makes no difference. Just don't overwrite/assign other values to it. That's all.  :)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...