Most of us write loops using index, but do you know that you can also write loops using objects?
Yes, it sounds like VB.Net but I'm talking about VB6 --- yes, it is possible.
To demonstrate, let's create a very simple Class module called "groceryItem"
Next, let's write the main module to demonstrate this loop.
For this demo, we'll need a Collection variable to hold lots of objects and in this case the objects will be instances of the groceryItem class.
Here's the code:
You'll notice there are 2 versions of loop --- one is the traditional, using index and the other is the non-traditional using the object (notice there is no use of an index here).
And here's the output, to show that it produces the same result:
Now you might be asking, what the heck is the newGroceryItem I used there :)
We'll since VB6 does not support the parameterized instantiation like VB.Net, we'll have to write our own "wrapper" to do that.
Now you can do the fully advanced technique of using Class Factories, but for this demo, a simple function would do.
And here's the function:
So go ahead and try this on one of your projects and you might like it :)
Got questions or suggestions? Post them below and I'll get back to you as soon as I can.