Search code examples
vb6control-array

vb6 - how to find largest element index in control array?


I am dynamically loading and unloading an array of command buttons on a form.

I can do this:

    Dim UnloadIndex As Integer
    For UnloadIndex = 1 To 20
        Unload frmMain.cmdAction(UnloadIndex)
    Next

But I don't always have 20 elements. Is there a way to loop through each one until it reaches the end?

I know I can use a global variable and track the value but I'm trying to avoid this.

Any suggestions please...


Solution

  • Use UBound() which returns the highest available subscript for the indicated dimension of an array.

    Dim UnloadIndex As Integer 
    For UnloadIndex = LBound(frmMain.cmdAction) To UBound(frmMain.cmdAction)
        Unload frmMain.cmdAction(UnloadIndex) 
    Next