Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Enhanced For-Next loops

Total Hit ( 2946)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


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:

Click here to copy the following block
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:

Click here to copy the following block
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:

Click here to copy the following block
Dim v As Variant
For Each v In Array("Spring", "Summer", "Fall", "Winter")
  PrintSaleReport v
Next



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.