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
   i = i  + 1
  x(i) = "this is line " & i
  goto read_data
  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

Who is Participating?
purquizConnect With a Mentor Commented:
Note that line :
if curr_index > 50 then

should be:

if curr_index > ubound(x) then

Now, I have an example that works by reading a text file line by line, and resizing the array as needed.

dim x() as string   ' define this as dynamic array
dim curr_index as long, lineNumber as long 
dim s1 as string
dim Fnumber as integer
dim Fname as string
open Fname for input as #Fnumber
  do while not eof(fnumber)
    line input#fnumber, s1
    redim preserve x(curr_index) as string
close #Fnumber
msgbox "The X() array now has "+cstr(ubound(x)+1)+" items."

Open in new window

clarkscottConnect With a Mentor Commented:
You can specify a dynamic 'max of array' and use what you need.
dim x(1 to 1000) as string

This will allow up to 1000 items but will only use (memory) what's needed.

There's still no way around setting a limit.  Arrays are maintained in memory and you must tell VB how much memory should be 'set aside'.

Scott C
advfinanceConnect With a Mentor Commented:
Is this what you were after?
Dim x(100) As String
Dim Bob
For Each Bob in x
  x = "this is a bit of text"
Next Bob
Dim x(100) As String
Dim i As Long
For i = LBound(x) To UBound(x)
  x(i) = "this is line " & i
Next i

Open in new window

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

mshox1Author Commented:
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)

   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
 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.
clarkscottConnect With a Mentor Commented:
redim PRESERVE X(100)
or whatever value you need to increase to
Scott C
mshox1Author Commented:
thank you very much for everyone's input.  this is veyr useful
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.