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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no direct equivalent in VB 6 to the generic
List<T>found in VB.NET. However, there is such a thing as aCollectionin VB 6, which provides similar functionality. The primary difference is that a VB 6Collectionis not strongly-typed, which means that all objects are stored asVariantsin 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 aCollectionand 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
Collectionin a class, where the only access to the internalCollectionis 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
Collectionobject yourself, in the form of public methods on your custom collection.Here’s an example of how you might go about that:
Note that in order to set the custom collection’s
Itemproperty as the collection’s default method (like the built-inCollectionobject), 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 Eachsyntax (also like the built-inCollectionobject), you can add aNewEnumfunction to your custom class: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”