Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1530

To Classes, a HashList and a refArray

$
0
0
This is a new idea. First the refArray class used for UDT substitute, by a simple way. WE make each "row" of refArray as a simple value or an array (we can choose specific type). If we want to write to an index above upperlimit then the class expand the arrays. We can't reduce but we can change with a fresh one, maybe a different type. We have Copy function, so we get all arrays in as a copy.

This is a part of an example in zip file. Normally we can't handle String() arrays or Long() arrays, using collections. So the refArray can be used for these situations. As you see we can define items, and arrays.


Code:

Dim c As New Collection, i As Long
c.Add New RefArray, "One"
c("One")(0) = "This is a string"
For i = 1 To 9
    c("One")(i) = i ^ 2
Next i
c("One")(10) = "Hello There"


c("One").DefArrayAt 11, vbByte, 1000
Debug.Print TypeName(c("One")(11)) = "Byte()"
c("One").DefArrayAt 13, vbString, 1000
Debug.Print TypeName(c("One")(13)) = "String()"


The second class, is the HashList which use the refArray, to make lists with keys (string or numbers), of type string, variant, byte, integer, long, currency (for decimal use variant). So there is one class for some different types. The new approach here is the use of byte size for hashcode for under 150 items, then if we add more the class change the hashcode to integer size, and after 21500 items change to long size. Also we can copy the structure (keys, data, and hash data) with one statement, creating a new list. You can change in refArray the way you have to copy special objects.


We can use same keys, and we can iterate through all items. Here we use numbers for keys, so we want to iterate like a sparse array. But we have same keys, so there is a way to find all items with same keys. And this example show this.

The Find() method can work for key only (we may have string and number keys), or we can use one or more of three optional, the value, the position, the pointer to other (which have the same hash code, but is rare except for same keys).
Also we can prepare the list before the use to have plenty of space, to avoid auto rehash, if we know about the size we have to use.

There is no sort, or delete for items (we can use clear, and then we can change the type of the item of the list). There is a Create function which create lists like this: Set list1 = list1.create("Hello", 1000, "bravo") for variant type.


Code:

Dim list1 As New HashList, there, i As Long, j As LongDim list2 As New HashList
' list1.Prepare 20 ' optional
For i = 1 To 20
list1.Add "George", 10&
list1.Add "John 5", 5&
list1.Add "George 2", 10&
list1.Add "John 125", 125&
Next
Dim lookother
For i = 1 To 150
    lookother = Empty
   
    Do
    If list1.Find(i, , there, lookother) Then
     
        If lookother > 0 Or list2.Count Then
            list2.Add list1(there), there
        Else
            Debug.Print "Key: " & i
            Debug.Print "Found at pos: " & there
            Debug.Print "Data: " & list1(there)
        End If
        list1(there) = list1(there) + "!!!"
    End If
    Loop Until lookother = 0
    If list2.Count > 0 Then
        For j = list2.Count - 1 To 0 Step -1
            Debug.Print "Key: " & i
            Debug.Print "Found at pos: " & list2.KeyNumeric(list2.Key(j))
            Debug.Print "Data: " & list2(j)
        Next j
        list2.Clear
    End If
Next i
Exit Sub
For i = 0 To list1.Count - 1
    Debug.Print list1.KeyNumeric(list1.Key(i)) & ": " & list1(i), i
Next i
End Sub

That's all for now.
Attached Files

Viewing all articles
Browse latest Browse all 1530

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>