We help IT Professionals succeed at work.

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

mshox1
mshox1 asked
on
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

Comment
Watch Question

CERTIFIED EXPERT
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
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

Author

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.
CERTIFIED EXPERT
Commented:
redim PRESERVE X(100)
or whatever value you need to increase to
Scott C
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
 
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

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.