Make LBound() and UBound() even more efficient

In a previous tip, we showed you how to loop through an array with
the LBound() and UBound() function. The code we provided used these
two functions in the For...Next statement itself, like this:

For x = LBound(MusicGenres) To UBound(MusicGenres)

where MusicGenres was the array we wanted to traverse.

As many astute tipsters pointed out, howevever, when you loop
through large arrays it's much more efficient to set variables to the
LBound() and UBound() function results and use these variables inside
the loop instead. This way, Visual Basic only wastes time performing
the functions once rather than many times.

So the code would then become

lctr = LBound(MusicGenres)
uctr = UBound(MusicGenres)
For x = lctr To uctr
    Debug.Print MusicGenres(x)
Next x

You might also like...

Comments

ElementK Journals

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne