By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

For Each v in Collection

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* "Michael D. Ober" <mdo.@.wakeassoc..com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2

P: n/a
"Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
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
Nov 21 '05 #3

P: n/a
Thanks Jeremy. That's exactly the information I needed.

Mike Ober.

"Jeremy" <th***********@hotmail.com> wrote in message
news:JO******************@twister.tampabay.rr.com. ..
"Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #4

P: n/a
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

"Jeremy" <th***********@hotmail.com> wrote in message
news:JO******************@twister.tampabay.rr.com. .. "Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #5

P: n/a
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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
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

"Jeremy" <th***********@hotmail.com> wrote in message
news:JO******************@twister.tampabay.rr.com. ..
"Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
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


Nov 21 '05 #6

P: n/a
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


"Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl... 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.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
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

"Jeremy" <th***********@hotmail.com> wrote in message
news:JO******************@twister.tampabay.rr.com. ..
"Michael D. Ober" <mdo.@.wakeassoc..com> wrote in message
news:%2*****************@TK2MSFTNGP09.phx.gbl...
> 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



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.