Jump to content

Const Arrays


cappy2112
 Share

Recommended Posts

right in help

Dim / Global / Local / Const

--------------------------------------------------------------------------------

Declare a variable, a constant, or create an array.

Dim [Const]$variable [ = initializer ]

Dim [Const] $array[subscript 1]...[subscript n] [ = initializer ]

Parameters

const [optional] If present, the Const keyword creates a constant rather than a variable.

$variable The name of the variable to declare.

initializer The value that will be initially assigned to the variable. A Const must include the initializer.

subscript The number of elements to create for the array dimension, indexed 0 to n-1.

8)

NEWHeader1.png

Link to comment
Share on other sites

maybe this

#include <Array.au3>

Dim $avArray[8] 
$avArray[0] = 7
$avArray[1] = "Brian"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Christa"
$avArray[5] = "Rick"
$avArray[6] = "Jack"
$avArray[7] = "Gregory"

_ArrayDisplay( $avArray, "_ArrayDisplay() Test" )
Exit

8)

NEWHeader1.png

Link to comment
Share on other sites

maybe this

#include <Array.au3>

Dim $avArray[8] 
$avArray[0] = 7
$avArray[1] = "Brian"
$avArray[2] = "Jon"
$avArray[3] = "Larry"
$avArray[4] = "Christa"
$avArray[5] = "Rick"
$avArray[6] = "Jack"
$avArray[7] = "Gregory"

_ArrayDisplay( $avArray, "_ArrayDisplay() Test" )
Exit

8)

Now try it with const- see what happens
Link to comment
Share on other sites

it gives the answer

Can pass constants by reference only to parameters with "Const" keyword.:

_ArrayDisplay( $avArray[1], "_ArrayDisplay() Test" )

_ArrayDisplay( ^ ERROR

dim'ed arrays are changable... const'ed arrays are not ( too many rules )

AND... why fight it.. use dim???????????

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

Global Const $MyAvArray_1 = "Brian"
Global Const $MyAvArray_2 = "Jon"
Global Const $MyAvArray_3 = "Larry"
Global Const $MyAvArray_4 = "Christa"
Global Const $MyAvArray_5 = "Rick"
Global Const $MyAvArray_6 = "Jack"
Global Const $MyAvArray_7 = "Gregory"

Local $ShowConstants
For $i = 1 To 7
    Local $Const = Eval("MyAvArray_" & $i)
    $ShowConstants = $ShowConstants & $Const & @LF
Next
MsgBox(0, 'Constant Test', $ShowConstants)
Exit

Edit:

Damn Code Tags!!

Edit2:

This way you can make Global/Local/Dim constants inside the For loop also.

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

  • Moderators

like i said... thats the hard way

and it doesn't want to work with all of the array functions available in Autoit

8)

I'm not denying it's the hard way, and his approach is not one I would take or understand why it needs/wants to be taken.

I was peddling around with it myself, and this was the only solution I could find for his situation.

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

  • Developers

it gives the answer

dim'ed arrays are changable... const'ed arrays are not ( too many rules )

AND... why fight it.. use dim???????????

8)

This error is due to a Byref on the _ArrayDisplay() parameter that should not be there. (Will be fixed in the next version

This works fine :

Const $avArray[7] = ["Brian", "Jon", "Larry", "Christa", "Rick", "Jack", "Gregory"]
_ArrayDisplay( $avArray, "_ArrayDisplay() Test" )
;
Func _ArrayDisplay($avArray, $sTitle)
    Local $iCounter = 0, $sMsg = ""
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    
    For $iCounter = 0 To UBound($avArray) - 1
        $sMsg = $sMsg & "[" & $iCounter & "]    = " & StringStripCR($avArray[$iCounter]) & @CR
    Next
    
    MsgBox(4096, $sTitle, $sMsg)
    SetError(0)
    Return 1
EndFunc  ;==>_ArrayDisplay

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

This error is due to a Byref on the _ArrayDisplay() parameter that should not be there. (Will be fixed in the next version

Rule #1 of working with arrays: Never pass an array by value.

Just make the function definition take a Const ByRef array:

Func _ArrayDisplay(Const ByRef $avArray, $sTitle)
Link to comment
Share on other sites

  • Developers

Rule #1 of working with arrays: Never pass an array by value.

Just make the function definition take a Const ByRef array:

Func _ArrayDisplay(Const ByRef $avArray, $sTitle)
@Valik, Is it just for efficiency not having to copy the Array in memory but just passing a pointer or is there also another reason ? Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Global Const $MyAvArray_1 = "Brian"
Global Const $MyAvArray_2 = "Jon"
Global Const $MyAvArray_3 = "Larry"
Global Const $MyAvArray_4 = "Christa"
Global Const $MyAvArray_5 = "Rick"
Global Const $MyAvArray_6 = "Jack"
Global Const $MyAvArray_7 = "Gregory"

Local $ShowConstants
For $i = 1 To 7
    Local $Const = Eval("MyAvArray_" & $i)
    $ShowConstants = $ShowConstants & $Const & @LF
Next
MsgBox(0, 'Constant Test', $ShowConstants)
Exit

Edit:

Damn Code Tags!!

Edit2:

This way you can make Global/Local/Dim constants inside the For loop also.

Thanks but, This isn't an array

Edited by cappy2112
Link to comment
Share on other sites

This works fine :

Const $avArray[7] = ["Brian", "Jon", "Larry", "Christa", "Rick", "Jack", "Gregory"]
This is what I was after. Although I wanted to use enums as indices in the assignnet, but I don't think that will work. No problem. I'll declare it like this and just use the enums to get the values of the consts.

Thanks!

Link to comment
Share on other sites

A good reason why ALL arguments should be implicitly passed ByRef, unless the ByVal keyword is explicitly used.

Wrong. Only arrays and parameters that need to be modified should be ByRef, everything else should be done by value. If you go around arbitrarily making everything ByRef, then you will always have to pass a variable to a function and won't be able to pass a literal:

Local $temp = "Must use temporary variable."
Function1($temp)

Func Function1(ByRef $value)
    MsgBox(4096, "", $value)
EndFunc

Versus:

Function2("This works without the temporary.")

Func Function2($value)
    MsgBox(4096, "", $value)
EndFunc
Link to comment
Share on other sites

  • Moderators

Thanks but, This isn't an array

You're right in the traditional sense, but if you can't accomplish what you want with an array, this will work as a nasty work around. Open your mind up a bit, look at your array, look at my example, and then take it from there.

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

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