Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Problem with array of records not being created

Author
Message
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 19th Jul 2015 03:09
The following code snippet is from a file that is included in `main.agc`:



The code compiles correctly, but when I execute the application, I get an error:

`Array index out of bounds, Index:1, Array Length: <Empty> in line 45` (this is the "EngDat[1].base_offset = hex_file_y / 8.0" line). The error message makes it sound like the array was not created, and I don't know why this would be the case. I have the `Hands on AppGameKit BASIC` book and I don't see what I am doing differently from the text's examples on array and records. Unless arrays aren't supposed to have global scope, but I thought they did.

Goo Goo G'Joob!
theUsual
4
Years of Service
User Offline
Joined: 13th Jul 2015
Location: Norway
Posted: 19th Jul 2015 16:59
Arrays start from index 0, and count from there. We humans count from 1.

When you write EngDat[1], you are making an array of one element. EngDat[1] = [0]. I you want to access the first value in the array, you have to type [0] as the index, as the COMPUTER counts from 0, and HUMANS count from 1. It's a little tricky to get used to in the start

Example:


By making an array like this offsetArray[6], you are adding 6 elements. The first element would have the 0th index, and the last element would have the 5th index. When you are trying to get the 7th element of an array that only has 6 elements, you the the "Out of Bound"-error.



Hope this helped you out
Ancient Lady
Valued Member
15
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 19th Jul 2015 20:15
That is strange.

It must be a change in AppGameKit V2.

In AppGameKit V1, when you created an array with length of 1, you actually got two. The one at zero index and the one at one index.

Cheers,
Ancient Lady
theUsual
4
Years of Service
User Offline
Joined: 13th Jul 2015
Location: Norway
Posted: 19th Jul 2015 21:01 Edited at: 19th Jul 2015 21:03
There is something odd here yes.

Take a look at this:


Edit: Or maybe it's correct. I'm totally confused right now..
okee
5
Years of Service
User Offline
Joined: 1st May 2014
Location: Ireland
Posted: 19th Jul 2015 21:17
From V2 help

Quote: "Note that because arrays start at index 0 and allow the size parameter as a valid index, an array defined as "DIM myArray[2]" or "myArray as integer[2]" would have 3 elements (0,1, and 2) but array.length will return "2" to reflect the size parameter that was used to declare it. Therefore an array of length 0 has one element and an empty array will return the length "-1"."


So it still works the same, but myarray.length can be a bit confusing
Ancient Lady
Valued Member
15
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 19th Jul 2015 22:02
The error is correct.

The fifth line is attempting to assign a value to an array location that doesn't exist.

The array is dimmed with 2, yielding three locations indexed by 0, 1 and 2.

Cheers,
Ancient Lady
Eqqman
4
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 20th Jul 2015 07:36
Thanks all.

I did attempt to index element 0 at first, but received the same error. I still don't know why this didn't work, but I worked around it by exchanging the `dim` keyword for `global` on the array declaration. After that things were fine.

Goo Goo G'Joob!

Login to post a reply

Server time is: 2019-09-15 08:43:14
Your offset time is: 2019-09-15 08:43:14