I want to know if exist a equivalent 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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With