Practical Web Programming

Monday, January 28, 2008

How to Handle Control Arrays with Index Holes in Between in Visual Basic

Here's simple tutorial on how you can handle control arrays with index holes in between.

Control array is one of the best feature of Visual Basic. However, this can cause runtime errors if you are not careful, especially if they have missing elements or index in between.

To handle control arrays with sequence index, you can use this method.

Dim intX as integer
For intX = Text1.LBound to Text1.UBound
MsgBox Text1(intX).Text

However, if they have holes in between them (Ex: Text1(0), Text1(1), Text1(3), Text1(4)), the above method spits an error. To avoid that situation, treat the array like a collection as below.

Dim txt as TextBox
For Each txt In Text1
MsgBox txt.Text
Next txt

That's it. Tamed control arrays. (^_^)


Recent Post