Page 1 of 1
What is wrong with this code?
Posted: Tue Mar 11, 2014 10:30 pm
by russellbdavis
Okay, I am rather new to this. I've been converting some VB code to PB and so far its been working fine. Until I introduced the use of structures.
This line of code is generating "[ERROR] Array index out of bounds."
tflights(i)\actype = actypes(typecnt)\actype ;this is the index to the actypes array
When this line of code is called upon, i = 1 and typecnt = say 13. Both of which are well within the bounds of the structure array.
If I enter tflights(1)\actype and click on [Display] with the debugger, it displays exactly what I am expecting.
If I enter actypes(13)\actype and click on [Display] with the debugger, it too displays exactly what I am expecting.
Is there some fundamental thing I am missing in the code?
Thanks.
Re: What is wrong with this code?
Posted: Tue Mar 11, 2014 10:43 pm
by srod
Need to see some code.
Try using ArraySize() to see exactly what the array upper bounds are.
Re: What is wrong with this code?
Posted: Tue Mar 11, 2014 10:58 pm
by russellbdavis
Actually, I've been using the debugger and using Arraysize;
===========================================
Number = Random(32767)
RandomSeed(Number)
newtype = Random(ArraySize(tindex()),1)
typecnt = tindex(newtype)
tmp = actypes(typecnt)\actype
Debug actypes(typecnt)\actype
Debug ArraySize(tindex())
Debug newtype
Debug ArraySize(tflights())
Debug "TFLIGHTS = "+Str(ArraySize(TFLIGHTS()))
===========================================
What will display is this;
tmp = nothing
actypes(typecnt)\actype = (the correct response)
ArraySize(tindex()) = 13 (the correct amount)
newtype = 1 - 58 (within range)
Arraysize(tflights()) = 13 (the correct size of the array)
i = 1 (which is the first element of the tflights() structure
So, I am really baffled. When I attempt to assign directly from one structure to the other, is when the error occurs:
tflights(i)\actype = actypes(typecnt)\actype
both elements in the structures are the same thing; strings.
Re: What is wrong with this code?
Posted: Tue Mar 11, 2014 11:03 pm
by luis
Post some code showing the problem.
All your source OR part of it OR extract structures definitions / arrays / dimensioning code only and put together some equivalent snippet as long it shows the error and it's compilable.
Re: What is wrong with this code?
Posted: Tue Mar 11, 2014 11:04 pm
by IdeasVacuum
Your code snippet is not complete, we can't see if you have defined the structure or the arrays correctly.
Re: What is wrong with this code?
Posted: Wed Mar 12, 2014 12:05 am
by russellbdavis
Thanks guys. I did narrow the problem down to the variable (i). So I'm looking back through the code to see what is happening with i.
Re: What is wrong with this code?
Posted: Wed Mar 12, 2014 12:10 pm
by em_uk
There is no need to use Debug with so many times.
Add a breakpoint, then go into the debugger menu then take a look at variable viewer. You will be able to view the contents and size of your structure and compare this against any variables.
Re: What is wrong with this code?
Posted: Wed Mar 12, 2014 1:03 pm
by russellbdavis
Thanks...yes, I've been adding lots of breakpoints and examining variables, arrays, etc.
Re: What is wrong with this code?
Posted: Wed Mar 12, 2014 10:41 pm
by russellbdavis
Thanks for your feedback guys. I fixed the problem.
I needed to Redim tflights by 1 each time I added a new structured record and not use "i", but a unique integer representing the # of items in the array.
Re: What is wrong with this code?
Posted: Thu Mar 13, 2014 2:05 am
by IdeasVacuum
I needed to Redim tflights by 1 each time I added a new structured record
Linked List might serve you better.