Qbasicnews.com
March 30, 2020, 10:12:00 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: subscript out of range error  (Read 2096 times)
jgr
Forum Regular
**
Posts: 131


« on: January 18, 2005, 12:50:22 PM »

Code:


REDIM addressarray(numofrecs1,3)
addresscounter=0
For i=1 to numofrecs1
   GET #1 i,address
    lastnametemp1$=MID$(address.lastname,1,spp)
    lastnametemp2$=MID$(startingpoint$,1,spp)

    IF startingpoint$="a" + SPACE$(length-1) then '*** Search All Names
         addresscounter=addresscounter+1
         addressarray$(addresscounter,1)=address.lastname
         addressarray$(addresscounter,2)=address.firstname
         addressarray$(addresscounter,3)=STR$(i)
    END IF

    IF lastnametemp1$=lastnametemp2$ THEN  '*** Search Specific
         addresscounter=addresscounter+1
         addressarray$(addresscounter,1)=address.lastname
         addressarray$(addresscounter,2)=address.firstname
         addressarray$(addresscounter,3)=STR(i)
    END IF


*****  Problem

While in the first if statement (Search the entire file) works fine as long as numofrecs1 is only 10 or less.  If more then 10 then I get a subscript out of range error when I get to the 11th record on the addressarray$(addresscounter,1)=address.lastname statement
Logged
dadsherm
Member
*
Posts: 75


« Reply #1 on: January 18, 2005, 03:02:47 PM »

jgr,

The problem comes from your not DIMentioning the size of "numofrecsl". So QB uses the default of 10.
If you wish to go larger(greater) than 10 you will need to DIMmention "numofrecsl to  a greater size.
If you are not sure of what that size will be then there are ways to handle this, but for right now I suggest you set it to 100.
***** EXAMPLE *****
DIM numofrecsl (100)
REDIM addressarry (numofrecsl,3)
'rest of your program goes here
This should give you plenty of memory set aside for your project.

I hope this helps. :wink:
Logged

adsherm
jgr
Forum Regular
**
Posts: 131


« Reply #2 on: January 18, 2005, 03:45:55 PM »

numofrecs1 is set to the number of records in the file.  Thus I wanted to set the array size to the same.  So if the numofrecs1=12 then why can't you just say dim xxxx$(numofrecs1,3)  Huh  Or are you not allowed to use variables in the dim and redim??  If not then I will just dim it to something that I know will be large enough.

Thanks
Logged
jgr
Forum Regular
**
Posts: 131


« Reply #3 on: January 18, 2005, 03:59:22 PM »

ok I found one problem that I had

the redim statement was wrong

it was redim addressarray(numofrecs1,3)
but shoud be   redim addressarray$(numofrecs1,3)


but now I get the error message array already dimensioned on the redim statement.
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #4 on: January 18, 2005, 04:20:21 PM »

put a DIM arrayname(0) at the beginning of your code, then you can REDIM to your heart's content.
Logged
jgr
Forum Regular
**
Posts: 131


« Reply #5 on: January 18, 2005, 04:39:29 PM »

at the beginning of the program

option base 1
dim addressarray$(10,3)


in the body of the program

redim addressarray$(100,3)

and I get array already dimensioned error
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #6 on: January 18, 2005, 05:18:33 PM »

oops.  my bad.  make that first DIM a REDIM.
Logged
jgr
Forum Regular
**
Posts: 131


« Reply #7 on: January 18, 2005, 05:53:39 PM »

Thanks Meg, works fine now.   Not sure if really understand why to use redim when first defining the array.  Thanks again
Logged
dadsherm
Member
*
Posts: 75


« Reply #8 on: January 18, 2005, 07:34:08 PM »

O.k. I think I found the problem.
With using the number of records as 12, the first time thru it should say:
DIM numofrecs1 (12): DIM addressarray$(numofrecs1,3)
Since numofrecs1 = 12, this is the same as:
DIM addressarray$(12,3)
This means you have 12 records with 3 parts to each record.
(I just added that for those who may be reading this, but don't really understand.)
After that you should have:
REDIM numofrecs1(100) :REDIM addressarray$(numofrecs,3)
However, you really don't need to raise it to 100 since you already know how many files you have, so the  REDIM numofrecs1 isn't really needed, but the REDIM addressarray$(numofrecs1,3) would still be.
What Meg suggested was a good one. That way you can REDIM it as needed.

( I usually add 1 more to the count for a "cushion". So it would be 13 rather than 12, but that's my own doing as it's not really needed.)

Sorry for the "numofresl" in my first reply.
That's really what I thought it said. :oops:
Logged

adsherm
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!