For Each v in Collection

M

Michael D. Ober

In VB 6, the loop iterator v in the following code must be a variant.

dim v as variant
dim c as new collection
for each v in collection
...
next v

What is the general translation in VB 7.1 and VB 8 Beta 1? Also, is there
an easy way to force "v" to be an early bound variable. In VB 6 this can be
done by going through the rather convoluted creation of a collection class
that implements the _IEnum interface.

Thanks,
Mike Ober
 
H

Herfried K. Wagner [MVP]

* "Michael D. Ober said:
In VB 6, the loop iterator v in the following code must be a variant.

dim v as variant
dim c as new collection
for each v in collection
...
next v

What is the general translation in VB 7.1 and VB 8 Beta 1?

You can use the items' type directly in VB.NET.
 
J

Jeremy

Michael D. Ober said:
In VB 6, the loop iterator v in the following code must be a variant.

dim v as variant
dim c as new collection
for each v in collection
...
next v


Collection is by default non-typed (using type Object). If you were to use
a different type of collection, such as a Specialized.StringCollection, then
yes. For example:

'// .NET 1.1: Using a weak-typed Collection:
Dim C as New Collection
Dim strItem as String
....
For each V as Object In C
Debug.WriteLine DirectCast(V, String)
Next



'// .NET 1.1: Using a specialized collection:
Dim C as New Collections.Specialized.StringCollection
....
For each ThisString as String In C
Debug.WriteLine ThisString
Next



'// .NET 2: Use generics
Dim C as New Generics.Collection(Of String)

For Each ThisString as String in C
Debug.WriteLine ThisString
Next


HTH,
Jeremy
 
J

Jay B. Harlow [MVP - Outlook]

Jeremy (& Michael),
You do realize that if your weak-typed collection only contains strings you
can do:
'// .NET 1.1: Using a weak-typed Collection:
Dim C as New Collection
For each strItem as String In C
Debug.WriteLine strItem
Next

The For Each will do the DirectCast for you!

Which also means that if C contains something other then a String, you will
get an InvalidCastException!

Hope this helps
Jay
 
M

Michael D. Ober

Frequently they are simply strings, but there are a signficant number of
cases where I use

for each v in c
set obj = v
...
next v

It appears that this will become

for each obj as objclass in c
...
next obj

This is probably the one area that I absolutely hate about VB 6 - collection
iterators always require variants.

Mike Ober.
 
J

Jay B. Harlow [MVP - Outlook]

Michael,
It appears that this will become

for each obj as objclass in c

Correct the 'variable' that you are using with the For Each can be strongly
typed to the actual type in the collection! Even when you have a collection
of objects, such as VB.Collection & ArrayList.

VB.NET will do a DirectCast to the type of the variable for you.
This is probably the one area that I absolutely hate about VB 6 - collection
iterators always require variants.
I "hate" the fact that VB6 doesn't have constructors more, but yes, now that
you reminded me, this one is way up there also.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top