help with subscript out of range error 
Author Message
 help with subscript out of range error

Quote:

> i got a subscript out of range with this code.  <<<<<< marks
> the line the error was on.  i used the same code just with
> different variables.  the variables are the same type but
> smaller number in the second copy of the code.  how come the
> first copy creates no error but the second copy does? heres the
> code:
> ===================================================================
> i% = 1
> DO
>    IF fileSize! <= fullPartSize! THEN
>       part(i%) = fileSize!
>       part(0) = i%
>       GOTO loopOneDone
>    ELSE
>       part(i%) = fullPartSize!
>       fileSize! = fileSize! - fullPartSize!
>       part(0) = i%
>       i% = i% + 1
>    END IF
> LOOP
> loopOneDone:
> partSize! = part(part(0))
> i% = 1
> DO
>    IF partSize! <= fullTransferSize! THEN
>       transfer(i%) = partSize!
>       transfer(0) = i%
>       GOTO loopTwoDone
>    ELSE
>       transfer(i%) = fullTransferSize!
>       partSize! = partSize! - fullTransferSize!

        transfer(0) = i%
Quote:
>       i% = i% + 1
>    END IF
> LOOP
> loopTwoDone:
> ===================================================================

-----------------------------------------------------------------
The most common reason for a "Subscript out of range error" is
using an array index which has a value less than the lower bound
or greater than the upper bound of an array.  Assuming this was
the cause of your error message, more information is needed
before an exact cause can be determined.  At the very least, you
have to state the dimensions of the arrays "part" and "transfer"
as well as the possible range of values for the variables used to
calculate the indices; "partSize!", "fullTransferSize!",
"fileSize!", and "fullPartSize!".

Someone else has already commented on the use of "GOTO" to exit
the loops.  While I don't think it's use is illegal, it is poor
programming style.  You should really revise your code.  From
what you have posted, I would guess that the first loop is an
attempt to deal with the possibility that the contents of a file
will not fit in available memory.  What puzzles me is the second
loop.  It would seem that you're trying to deal with the
possibility that the amount of data which can be transfered out
of memory at any one time is less than the amount of data
currently held in memory.  What bothers me is unless
fileSize! is less than fullPartSize! or fileSize! is evenly
divisable by fullPartSize!, you're only breaking up the "leftover"
part, which is less than fullPartSize!.

Perhaps this is a problem due to lack of context . . . this may
be exactly what you want to do and I can't see that because I
haven't seen the rest of the program.  If that's true, I suggest
you do away with loops and arrays altogether and learn the use of
the integer division and modulus operators.  For example,
assuming a default data type of long integer, one functional
equivalent of your code would be:

   NumberofFullParts = fileSize\fullPartSize
   LeftOverBytes = partSize MOD fullPartSize
   if LeftOverBytes=0 then BTS=fullPartSize else BTS=LeftOverBytes
   NumOfFullTransferPacks = BTS\fullTransferSize
   LeftOverBytestoTransfer = BTS MOD fullTransferSize

-----------------------------------------------------------------
Derek Asari



Sun, 21 Oct 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help! Error # 9 - Subscript out of range

2. HELP! Subscript Out Of Range error referencing array

3. Subscript out of range error

4. Subscript out of range error

5. Subscript out of range error

6. AddShapeRecordDetected Error: Subscript out of range

7. Defining an array and getting Subscript Out of Range Error

8. Problems With FlexGrid And Subscript Out Of Range Error

9. runtime error 9, subscript out of range

10. Subscript out of range (Error 9)

11. Redim causes Subscript Out of Range (Error 9)

12. Subscript out of range error problem

 

 
Powered by phpBB® Forum Software