Results 1 to 8 of 8

Thread: VB 6 Array Problems

Threaded View

  1. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: VB 6 Array Problems

    Alternatively, if you do want to separate by length you could use a UDT array with a Dynamic Array as a member, rather than a 2D array. Also, 'ReDim Preserve' is fairly slow so minimising the number times you use it is a good idea.

    The code below is an example of using a UDT and 'over dimensioning' the Dynamic Arrays such that they only have to be ReDim Preserve(d) once. The use of Constants for the Minimum and Maximum number of characters in a word gives an opportunity to extend easily. (eg if you want to include 7 letter words, just change the Constant 'MAX' to 7)
    Code:
    Option Explicit
    
    Private Const MIN As Integer = 3    'Minimum number of characters in a word
    Private Const MAX As Integer = 6    'Maximum number of characters in a word
    
    Private Type Words
        Count As Long
        List() As String
    End Type
    
    Private uWords(MIN To MAX) As Words
    
    Private Sub Command1_Click()
    Dim intFile As Integer
    Dim intI As Integer
    Dim lngI As Long
    Dim intLen As Integer
    Dim strData As String
    Dim strRecords() As String
    intFile = FreeFile
    '
    ' Open the data file, read the entire contents
    ' and split into records
    '
    Open "C:\dashed.txt" For Input As intFile
    strData = Input(LOF(intFile), intFile)
    Close intFile
    strRecords = Split(strData, vbNewLine)
    '
    ' 'Over Dimension' each dynamic array in the Words List(s)
    '
    For intI = MIN To MAX
        ReDim uWords(intI).List(UBound(strRecords))
    Next intI
    '
    ' Populate the Words List(s)
    ' Note that any blank lines in the data file are ignored
    ' and any word less than 'MIN' or greater than 'MAX' characters
    ' (after removing leading and / or trailing spaces) is ignored
    '
    For lngI = 0 To UBound(strRecords)
        strRecords(lngI) = Trim$(strRecords(lngI))
        If strRecords(lngI) <> vbNullString Then
            intLen = Len(strRecords(lngI))
            If intLen >= MIN And intLen <= MAX Then
                uWords(intLen).List(uWords(intLen).Count) = strRecords(lngI)
                uWords(intLen).Count = uWords(intLen).Count + 1
            End If
        End If
    Next lngI
    '
    ' ReDim the Words List(s) to the actual number
    ' of elements populated
    '
    For intI = MIN To MAX
        If uWords(intI).Count > 0 Then
            ReDim Preserve uWords(intI).List(uWords(intI).Count - 1)
        End If
    Next intI
    Command2.Enabled = True
    MsgBox "UDT Populated"
    End Sub
    
    Private Sub Command2_Click()
    '
    ' Output the UDT to the Immediate Window
    '
    Dim intI As Integer
    Dim lngJ As Long
    For intI = MIN To MAX
        Debug.Print CStr(intI) & " Letter Words:(" & CStr(uWords(intI).Count) & ")"
        If uWords(intI).Count > 0 Then
            For lngJ = 0 To UBound(uWords(intI).List)
                Debug.Print , uWords(intI).List(lngJ)
            Next lngJ
        End If
        Debug.Print
    Next intI
    End Sub
    
    Private Sub Form_Load()
    Command2.Enabled = False
    End Sub
    Last edited by Doogle; Oct 14th, 2011 at 01:07 AM. Reason: Added Constants

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width