Link to home
Start Free TrialLog in
Avatar of mshox1
mshox1

asked on

correct way to use dim x(100) as string, array string, and control the out of bound array error

I need to know how to use correctly the way array variable for the string type.
here is what I do to define an array of string, and then loading the data to the array, I need to know how to control the out of range error correctly in the VB6 enviroments.

dim x(100) as string
dim i as integer   ' index value of the string array
Here is an example:
on error goto err_outofbound
 read_data:
   i = i  + 1
  x(i) = "this is line " & i
  goto read_data
err_outofbound:
  debug.print err.description & err.number
  resume              ' --> want to find out


of cources, if i > 100, it will be out of bound.
However, VB6 do not like this and will not be able to "trap" the out of range error....  after that,
after that we get nasting memory access issues...

Just wondering  what is the best practice of using this in VB6?
do they have some thing at beging of the code
like Option boundcheck?
just like Option Explicit   --> this will force you to declair every variable used in the program

Please help

SOLUTION
Avatar of clarkscott
clarkscott
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mshox1
mshox1

ASKER

I think what I am trying to do is how to use the Dynamic array in VB6 (for lacking of better word), because I do not know how much size I need in ahead of time.  (the applicaiton will read external data file send to me externally, I need to adjust the size when I read the file).
Here is what I just researched on the internet.  would like to see your comments:  (I have not test it yet)

Requirement:
   1. do  not know the size of the array will be, before execution the code
   2. in the event, size goes out of initiate setting limit, need to be able to expand it, but want to keep
      the current value.
   3. reset the size when execute the routine again.

dim x() as string   ' define this as dynamic array
dim curr_index as integer
dim s1 as string

redim x(50)    ' initial define the size
 curr_index = -1
  read record s1 from file
 curr_index = curr_index + 1
  if curr_index > 50 then
      ' need to increase the size, but want to preserve the previous value
      redim preserve x(100)     ' increase 50, but keep the data for this transaction
     endif
 x(curr_index) = s1
  goto read-next-record    ' this is pudo code

I think this way it will works.

Please let me know if above "pudo code" will do its job.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mshox1

ASKER

thank you very much for everyone's input.  this is veyr useful