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

mshox1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clarkscottCommented:
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
0
advfinanceCommented:
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
 
Or
 
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

0
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)

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.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

clarkscottCommented:
redim PRESERVE X(100)
or whatever value you need to increase to
Scott C
0
purquizCommented:
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
 
Fnumber=freefile()
Fname="C:\somefolder\somefile.txt"
 
lineNumber=0
 
open Fname for input as #Fnumber
  do while not eof(fnumber)
    line input#fnumber, s1
    linenumber=linenumber+1
    curr_index=linenumber-1
    redim preserve x(curr_index) as string
    x(curr_index)=s1
  loop
close #Fnumber
 
msgbox "The X() array now has "+cstr(ubound(x)+1)+" items."

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mshox1Author Commented:
thank you very much for everyone's input.  this is veyr useful
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.