When you use a For...Next loop to iterate through an array, you may
be inclined to hard code the starting and ending counter values. For
instance, suppose you created the following arrayMusicGenres = Array("Blues", "Classic Rock", "Country",
"Dance", "Disco", "Funk", "Grunge",
"Hip -Hop",
"Jazz", "Metal", "New Age", "Oldies",
"Other")
you might think to loop through the array like soFor x = 0 To 12
Debug.Print MusicGenres(x)
Next x
However, because you may want to add more items to the array at a later
time, it's best to use the LBound() and UBound() functions to delimit
the counter's boundaries, as inFor x = LBound(MusicGenres) To UBound(MusicGenres)
Debug.Print MusicGenres(x)
Next x
This way, no matter how many times you add items to the array, you
won't need to modify the For...Next loop at all. Also, keep in mind
that you don't need to subtract 1 from the UBound() value because the
function returns the array's largest available subscript NOT the number
of items in the array.
UBound() and LBound()
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
VB 6 forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB 6 podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments