What is wrong with this code?

Just starting out? Need help? Post your questions and find answers here.
russellbdavis
User
User
Posts: 31
Joined: Wed Dec 02, 2009 4:50 pm
Location: Oklahoma City, OK
Contact:

What is wrong with this code?

Post 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.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: What is wrong with this code?

Post by srod »

Need to see some code.

Try using ArraySize() to see exactly what the array upper bounds are.
I may look like a mule, but I'm not a complete ass.
russellbdavis
User
User
Posts: 31
Joined: Wed Dec 02, 2009 4:50 pm
Location: Oklahoma City, OK
Contact:

Re: What is wrong with this code?

Post 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.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: What is wrong with this code?

Post 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.
"Have you tried turning it off and on again ?"
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

Your code snippet is not complete, we can't see if you have defined the structure or the arrays correctly.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
russellbdavis
User
User
Posts: 31
Joined: Wed Dec 02, 2009 4:50 pm
Location: Oklahoma City, OK
Contact:

Re: What is wrong with this code?

Post 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.
User avatar
em_uk
Enthusiast
Enthusiast
Posts: 366
Joined: Sun Aug 08, 2010 3:32 pm
Location: Manchester UK

Re: What is wrong with this code?

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

R Tape loading error, 0:1
russellbdavis
User
User
Posts: 31
Joined: Wed Dec 02, 2009 4:50 pm
Location: Oklahoma City, OK
Contact:

Re: What is wrong with this code?

Post by russellbdavis »

Thanks...yes, I've been adding lots of breakpoints and examining variables, arrays, etc.
russellbdavis
User
User
Posts: 31
Joined: Wed Dec 02, 2009 4:50 pm
Location: Oklahoma City, OK
Contact:

Re: What is wrong with this code?

Post 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.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: What is wrong with this code?

Post by IdeasVacuum »

I needed to Redim tflights by 1 each time I added a new structured record
Linked List might serve you better.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply