The Class of
Collection in VB 6.
VB.NET offer many data structure like ArrayList,
Queue, Stack, HashTable,
Collection etc., to hand the
data, while VB 6.0 only support the Collection
object. In this article you will
learn how to use Collection to implement something similar to
Stack class in VB_NET.
Once you have created a
collection object, you can do any one of the following:
(1)Add
an element to the Collection uses the Add method.
(2)Remove
an element that uses the Remove method
(3)Find out how many elements the Collection contains with the
Count Property
(4)Return a specific element from the collection with the
Item Property
(5)
Use For Each … Next statement to show the element of a Collection
(6)
Use Set Obj=Nothing to clear
the items of a Collection
The
Add method syntax are like:
object.Add(Item, [Key],[Before].[
After])
For example, to add a
student order object to a collection using the student's
ID property as its key, you can make the
following call.
Dim StudentColl as new
Collection
StudentColl.Add(“C.H
Day”,”id_0”)
(a)Exmple of Add method of Collection
If you want to order an array of number, you
can use the powerful Add method of Collection., the following
snippet codes are the implement to reorder and remove duplicate of
an array.
Sub
Order_Duplictae(iType As Integer, ListIn As ListBox)
Dim IntegerColl As New
Collection
Set IntegerColl =
Nothing 'Initialize
IntegerColl.Add (-9999)
IntegerColl.Add (99999)
Dim iTestA As Integer,
iTestB As Integer
Dim i As Integer, j
As Integer
For i = 0 To
UBound(ArrayI)
For j = 1 To
IntegerColl.count - 1
If iType = 0
Then 'Order
If ArrayI(i) >=
Val(IntegerColl.Item(j)) And ArrayI(i) <= Val(IntegerColl.Item(j + 1))
Then IntegerColl.Add ArrayI(i), , , j
'Add item after j GoTo 100
End If
Else
'Remove duplicate
If ArrayI(i) > Val(IntegerColl.Item(j))
And ArrayI(i) < Val(IntegerColl.Item(j + 1)) Then
IntegerColl.Add ArrayI(i), , , j 'Add item
after j GoTo 100
End If
End If
Next j
100:
Next i
'Remove the 1st item & Lastest item
IntegerColl.Remove
(1)
IntegerColl.Remove (IntegerColl.count)
'show member of the
collection
For i = 1 To
IntegerColl.count
ListIn.AddItem
"Count= " & i & " ; " & IntegerColl.Item(i)
Next i
End Sub
(b)Implement of stack with Collection object
VB 6 does
not support the Stack object, but you can implement the stack method
using Collection object.
Here we use Collection to
Implement the Push() and Pop() of Stack()
'! Push data into a
collection(把資料放入堆疊中)
Private Sub
Push(value As Integer)
'integer value
If (Current > nTOP) Then
'! over the capacity of a
collection( 超過容量)
Form3.Picture1.Print
"Stack full."
Exit Sub 'Return
End If
Current = Current + 1
CollectMove.Add value
‘using Add method
'Picture1.Print "Push " &
value & " in"
End Sub
'! Pop data from a collection(從堆疊中取出資料)
Private Function Pop(value As Integer) As Integer
'integer value
If (Current <= 0) Then '!
nothing more to take off(已經沒有東西可以拿了)
Pop = 1
Exit Function 'Return
End If
value
= CollectMove.Item(CollectMove.Count)
CollectMove.Remove
CollectMove.Count
Current = Current - 1
Pop = 0
End Function
Figure 1 and figure 2 are the
result of the program of one player Knight Chess In VB 6.0, in this
program, I use Collection object to implement the stack method.

Figure 1

Figure 2
For more information please line:
www.chady169.url.tw
|