Library code snippets
UBound() and LBound()
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.
Related articles
Related discussion
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
-
Hyperterminal Data
by sengreen (1 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Another handy place to use the UBound is with the ParamArray option of subs and fuctions
Private Sub Command1_Click()
MyCode "a", "b", 1
MyCode "Z", 1, "y", 4, "X"
End Sub
Sub MyCode(ParamArray Data() As Variant)
For x = 0 To UBound(Data)
Debug.Print Data(x)
Next
End Sub
This thread is for discussions of UBound() and LBound().