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:
'(In the declarations section) Const DeltaDim=100 Dim MaxNum As Integer Dim x() As single, y() as single ______________________________________________________________________ Sub SomeName() 'This subroutine reads a (very large) number of x & y coordinates from a text file Dim ff As Integer Dim nl as integer Dim k as integer Dim TxtLin as string 'Initialize array dimension to some convenient value MaxNum=1000 Redim x(1 To MaxNum), y(1 To MaxNum) 'Initialize line counter nl = 0 ff = FreeFile '(FileName is assumed to be assigned elsewhere) Open FileName For Input As #ff While Not EOF(ff) Line Input #ff, TxtLin nl=nl+1 'This is the relevant section: incresing the 'arrays' dimensions as needed If nl > MaxNum Then MaxNum= MaxNum + DeltaDim ReDim Preserve x(1 To MaxNum), y(1 To MaxNum) End If End If 'In this example, lines are read that contain x 'and y coordinate values separated by tabs k=InStr(TxtLin,vbTab) x(nl)=Val(Left(TxtLin,k-1)) y(nl)=Val(Mid(TxtLin,k+1)) Wend Close #ff 'Release unused array space by keeping only 'the actual values just read from the file ReDim Preserve x(1 To nl), y(1 To nl) End Sub




Reply With Quote