vb6 equivalent to list<someclass>
I want to know if exist a equi开发者_如何学Pythonvalent of (.net)
list<somefixedclass>
in vb6
I know that exist collection in vb6 but it uses object (variant) instead of a specific object.
thanks.
There is no direct equivalent in VB 6 to the generic List<T>
found in VB.NET. However, there is such a thing as a Collection
in VB 6, which provides similar functionality. The primary difference is that a VB 6 Collection
is not strongly-typed, which means that all objects are stored as Variants
in the collection. In some cases, this can be beneficial, because it allows you to store many different types of data in the same collection, and in fact, VB uses this object internally. It's easy enough to use a Collection
and up-cast objects as they are retrieved from the class, but there's little you can do. It's not possible to implement strongly-typed collections in the VB runtime.
That being said, there is a workaround you can implement. Similarly to how collections were implemented in early versions of VB.NET before generics were introduced, you can wrap the Collection
in a class, where the only access to the internal Collection
is through methods that you expose from this class. This design pattern is commonly referred to as a "custom collection".
This does have the benefit of automatically handling casting, and alleviates the consumers of your code from having to remember to mind implementation details like this. It takes care of the (all too likely) possibility that you'll be looping through a collection at run-time that is only supposed to contain one type of object, but accidentally had a second, incompatible type of object added that causes your code to throw an exception. Of course, the disadvantage is that you have to re-implement most of the functionality already provided by the Collection
object yourself, in the form of public methods on your custom collection.
Here's an example of how you might go about that:
Public Class CustomerCollection
''#Internal collection, exposed by this class
Private m_Customers As Collection
Private Sub Class_Initialize()
''#Set up the internal collection
Set m_Customers = New Collection
End Sub
Public Sub Add(ByVal cust as Customer, Optional ByVal key as String)
''#Add the Customer object to the internal collection
If IsMissing(key) Then
m_Customers.Add cust
Else
m_Customers.Add cust, key
End If
End Sub
Public Property Get Count() As Integer
''#Return the number of objects in the internal collection
Count = m_Customers.Count
End Property
Public Sub Remove(ByVal index As Variant)
''#Remove the specified object from the internal collection,
''# either by its index or its key
m_Customers.Remove index
End Sub
Public Function Item(ByVal index As Variant) as Customer
''#Return the specified object from the internal collection,
''# either by its index or its key
Set Item = m_Customers.Item(index)
End Function
Public Sub Clear()
''#Removes all objects from the internal collection
Set m_Customers = New Collection
End Sub
End Class
Note that in order to set the custom collection's Item
property as the collection's default method (like the built-in Collection
object), you need to follow these steps in the VB 6 IDE:
From the "Tools" menu, click "Procedure Attributes"
Select the name of your custom class from the "Name" combo box.
When the dialog appears, click the "Advanced" button.
Select the "(Default)" item in the "Procedure ID" combo box.
Click "OK"
If you'd also like to allow enumeration of your custom class using the For Each
syntax (also like the built-in Collection
object), you can add a NewEnum
function to your custom class:
Public Property Get NewEnum() As IUnknown
''#Provides support for enumeration using For Each
Set NewEnum = m_Customers.[_NewEnum]
End Property
Once you've done that, you need to instruct VB to use this property:
As before, open the "Procedure Attributes" dialog from the "Tools" menu
Select the name of your custom class from the "Name" combo box.
When the dialog appears, click the "Advanced" button.
Type the number "-4" in the "Procedure ID" combo box.
Click "OK"
Here is our implementation of ArrayList. You can use it as a base (not through inheritance obviously but through composition as expressed in CodyGray's answer) for a strongly typed class, but if you don't need type safety it is much better than the Collection class.
Option Explicit
Private mavInternalArray() As Variant
Private mlArraySize As Long
Private mlCount As Long
Private mlGrowSize As Long
Private mfZeroIndex As Boolean
'---------------------------------------------------------------------------------------
' Procedure Clear
'---------------------------------------------------------------------------------------
Public Sub Clear()
Dim index As Long
For index = 0 To mlCount - 1
If IsObject(mavInternalArray(index)) Then
Set mavInternalArray(index) = Nothing
End If
Next index
mlCount = 0
End Sub
'---------------------------------------------------------------------------------------
' Procedure Swap
'---------------------------------------------------------------------------------------
Public Sub Swap(Index1 As Long, index2 As Long)
Dim vTmp As Variant
If IsObject(mavInternalArray(index2)) Then
Set vTmp = mavInternalArray(index2)
Else
vTmp = mavInternalArray(index2)
End If
If IsObject(mavInternalArray(Index1)) Then
Set mavInternalArray(index2) = mavInternalArray(Index1)
Else
mavInternalArray(index2) = mavInternalArray(Index1)
End If
If IsObject(vTmp) Then
Set mavInternalArray(Index1) = vTmp
Else
mavInternalArray(Index1) = vTmp
End If
End Sub
Public Property Get ZeroIndex() As Boolean
ZeroIndex = mfZeroIndex
End Property
Public Property Let ZeroIndex(fZeroIndex As Boolean)
mfZeroIndex = fZeroIndex
End Property
Public Property Get GrowSize() As Long
GrowSize = mlGrowSize
End Property
Public Property Let GrowSize(lNewSize As Long)
Debug.Assert lNewSize > 0
mlGrowSize = lNewSize
End Property
Private Sub Class_Initialize()
mlGrowSize = 50
mlArraySize = mlGrowSize
mfZeroIndex = True
mlCount = 0
ReDim mavInternalArray(0 To mlGrowSize - 1)
End Sub
'---------------------------------------------------------------------------------------
' Procedure Remove
'---------------------------------------------------------------------------------------
Public Sub Remove(index As Long)
Dim index2 As Long
For index2 = index To mlCount - 2
If IsObject(mavInternalArray(index2 + 1)) Then
Set mavInternalArray(index2) = mavInternalArray(index2 + 1)
Else
mavInternalArray(index2) = mavInternalArray(index2 + 1)
End If
Next index2
If mlCount <= 0 Then
Exit Sub
End If
mlCount = mlCount - 1
If IsObject(mavInternalArray(mlCount)) Then
Set mavInternalArray(mlCount) = Nothing
Else
mavInternalArray(mlCount) = False
End If
End Sub
'---------------------------------------------------------------------------------------
' Procedure Items
'---------------------------------------------------------------------------------------
Public Function Items(index As Long) As Variant
If Not mfZeroIndex Then
index = index - 1
End If
If index < mlCount And index >= 0 Then
If IsObject(mavInternalArray(index)) Then
Set Items = mavInternalArray(index)
Else
Items = mavInternalArray(index)
End If
End If
End Function
Public Sub SetItem(index As Long, Item As Variant)
If Not mfZeroIndex Then
index = index - 1
End If
If IsObject(Item) Then
Set mavInternalArray(index) = Item
Else
mavInternalArray(index) = Item
End If
End Sub
'---------------------------------------------------------------------------------------
' Procedure Add
'---------------------------------------------------------------------------------------
Public Function Add(vItem As Variant) As Long
mlCount = mlCount + 1
If mlCount > mlArraySize Then
mlArraySize = mlArraySize + mlGrowSize
ReDim Preserve mavInternalArray(0 To mlArraySize - 1)
End If
If IsObject(vItem) Then
Set mavInternalArray(mlCount - 1) = vItem
Else
mavInternalArray(mlCount - 1) = vItem
End If
Add = mlCount - 1
End Function
'---------------------------------------------------------------------------------------
' Procedure ItemArray
'---------------------------------------------------------------------------------------
Public Function ItemArray() As Variant
Dim vReturnArray As Variant
vReturnArray = mavInternalArray
ReDim Preserve vReturnArray(0 To mlCount - 1)
ItemArray = vReturnArray
End Function
Public Function Count() As Long
Count = mlCount
End Function
'---------------------------------------------------------------------------------------
' Procedure Insert
'---------------------------------------------------------------------------------------
Public Function Insert(index As Long, vItem As Variant) As Long
Dim index2 As Long
'Make sure array is large enough for a new item
mlCount = mlCount + 1
If mlCount > mlArraySize Then
mlArraySize = mlArraySize + mlGrowSize
ReDim Preserve mavInternalArray(0 To mlArraySize - 1)
End If
'Bump all the items with a higher index up one spot
If index >= mlCount - 1 Then
If IsObject(vItem) Then
Set mavInternalArray(mlCount - 1) = vItem
Else
mavInternalArray(mlCount - 1) = vItem
End If
Else
For index2 = mlCount - 1 To index + 1 Step -1
If IsObject(vItem) Then
Set mavInternalArray(index2) = mavInternalArray(index2 - 1)
Else
mavInternalArray(index2) = mavInternalArray(index2 - 1)
End If
Next index2
If IsObject(vItem) Then
Set mavInternalArray(index) = vItem
Else
mavInternalArray(index) = vItem
End If
End If
Insert = mlCount - 1
End Function
Public Sub Clone(ByRef cDestinationDynamicArray As clsDynamicArray)
Dim index As Long
If cDestinationDynamicArray Is Nothing Then
Set cDestinationDynamicArray = New clsDynamicArray
End If
cDestinationDynamicArray.Clear
For index = 0 To mlCount - 1
Call cDestinationDynamicArray.Add(mavInternalArray(index))
Next index
End Sub
Public Property Get NewEnum() As IUnknown
''#Provides support for enumeration using For Each
Set NewEnum = m_Customers.[_NewEnum]
End Property
EDIT: if Cody Gray's solution is too oversized for your needs, you might try instead the "poor man's list" solution, as follows:
Dim l() as somefixedclass
Redim l(0)
'...
'increase size dynamically:
Redim Preserve l(ubound(l)+1)
'...
Of course, a List<somefixedclass>
(in C#) or a List(Of somefixedclass)
in VB.NET is much more "user-friendly" because it has methods like Find, Remove, AddRange and some other helpful things. The old VB6 construct deals very badly with the "empty list" case. Not to forget, List<..> increasement has much better performance for big lists (size>1000).
VB6 is an ancient language. It doesn't contain template-like types as there are in modern languages (C++, C#, Java). So you will have to store your objects as Variants in the collection and then cast them back to your object type later.
Dictionary is the best way to contain any object.
精彩评论