473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

For Each v in Collection

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
6 4817
* "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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
11
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.