Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: Intermediate
Apr 20, 1999

Enhanced For-Next loops

At times you may need to execute the same block of statements for different values of a variable, but you can't use a straight For Next loop because the sequence of values is not regular. For example, you may need to repeatedly call a given procedure passing the first ten prime numbers as arguments:


DoSomething 2
DoSomething 3
DoSomething 5
DoSomething 7
DoSomething 11
DoSomething 13
DoSomething 17
DoSomething 19
DoSomething 23
DoSomething 29
Here is a better approach, based on the Array() function that builds a Variant array on the fly, and a For Next loop that iterates on it:


Dim v As Variant
For Each v In Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
    DoSomething v
Next
You can exploit this trick to build loops that aren't even based on numeric control variables:


Dim v As Variant
For Each v In Array("Spring", "Summer", "Fall", "Winter")
    PrintSaleReport v
Next
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date