I have been thinking about the most convenient way to use dynamic arrays so that memory can be efficiently used. The following code is an example borrowed from an application I'm working on. Aside from the fact that it doesn't care about the total amount of computer memory it seems to work very nicely.

However, I'd like to hear the gurus' opinion: is it formally correct or does it have some conceptual flaw that I have overlooked?

VB Code:
  1. '(In the declarations section)
  2. Const DeltaDim=100
  3. Dim MaxNum As Integer
  4. Dim x() As single, y() as single
  5. ______________________________________________________________________
  6.  
  7. Sub SomeName()    
  8. 'This subroutine reads a (very large) number of x & y coordinates from a text file
  9.     Dim ff As Integer
  10.     Dim nl as integer
  11.     Dim k as integer
  12.     Dim TxtLin as string
  13. 'Initialize array dimension to some convenient value
  14.     MaxNum=1000
  15.     Redim x(1 To MaxNum), y(1 To MaxNum)
  16. 'Initialize line counter
  17.     nl = 0
  18.     ff = FreeFile
  19. '(FileName is assumed to be assigned elsewhere)
  20.     Open FileName For Input As #ff
  21.         While Not EOF(ff)
  22.             Line Input #ff, TxtLin
  23.             nl=nl+1
  24. 'This is the relevant section: incresing the
  25. 'arrays' dimensions as needed
  26.             If nl > MaxNum Then
  27.                 MaxNum= MaxNum + DeltaDim
  28.                 ReDim Preserve x(1 To MaxNum), y(1 To MaxNum)
  29.                 End If
  30.             End If
  31. 'In this example, lines are read that contain x
  32. 'and y coordinate values separated by tabs
  33.             k=InStr(TxtLin,vbTab)
  34.             x(nl)=Val(Left(TxtLin,k-1))
  35.             y(nl)=Val(Mid(TxtLin,k+1))
  36.         Wend
  37.     Close #ff
  38. 'Release unused array space by keeping only
  39. 'the actual values just read from the file
  40.     ReDim Preserve x(1 To nl), y(1 To nl)
  41. End Sub