Invalid Index error when index is out of range

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Invalid Index error when index is out of range

21 Feb 2023, 15:04

Sorry, I didn't read the docs carefully enough, using Default wouldn't work:
Setting a default value does not prevent an error from being thrown when the index is out of range.
It would work in such a case:

Code: Select all

Array.Prototype.DefineProp("Default", {get:(*)=>""})
test := ["a",,"b"]
MsgBox test[2]
Unfortunately I don't know of any additional ways other than the ones already mentioned here.
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Invalid Index error when index is out of range

23 Feb 2023, 13:21

One of the ways that @swagfag suggested is to override the default Array's __Item.Get. You can do so as follows:

Code: Select all

OverrideArray()
OverrideArray() {
   Get := Array.Prototype.GetOwnPropDesc('__Item').Get
   Array.Prototype.DefineProp('__Item', {Get: (this, key) => this.Has(key) ? Get(this, key) : ''})
}

a := [1]
MsgBox a[1] " " a[2]
I hope this helps.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 65 guests