March 6, 2021
Hot Topics:

Discovering Arrays

  • By Karl Moore
  • Send Email »
  • More Articles »

One lovely keyword in the world of arrays is Erase. Or as I prefer to call it, Zap.

Zapping your array means, literally, killing it. Haha - Asta la Vista, Array!

I mean, don't get me wrong; it still exists. But it has no bally dimensions, no data, no anything. It's a void chasm of nothingness.

<Ed: How poetic!>

  • Add another Command Button to your Form
  • Change it's Caption property to: Erase Array

Getting used to this yet?

  • Insert the following line of code behind the button:
Private Sub Command7_Click()Erase FavouriteThingsEnd Sub

Try running your application. Redimension the array, put something into that sixth element, mess around here and there. Then zap it.

Now try editing the fourth element or some such. You'll get an 'Error 9 - Subscript Out Of Range' error. That's because you zapped your array, causing it to die and shrivel up into a tiny ball of steel no larger than a frog's wedding tackle.

Haha! The power of the Zapster!!

So you see, this keyword is great when you just want to forget everything you've done so far and start again. Jolly useful.

Top Tip: The Erase keyword completely zaps all dimensions of dynamic variables. But fixed-sized variables, as discussed in the first few pages of this tutorial always keep their initial dimensions and can't be changed in code.

To complete our mini project here, I'd like you to add one more chunk of code. This is a feature that will display the dimensions of your array.

  • Add another... yep.
  • Change it's Caption property to: Show Dimensions
  • Insert the following code behind the button:
Private Sub Command8_Click()On Error GoTo BoundErrCall MsgBox("Here are the dimensions of your array:" & _vbNewLine & "LBound: " & LBound(FavouriteThings) & _vbNewLine & "UBound: " & UBound(FavouriteThings), vbInformation)Exit SubBoundErr:If Err.Number = 9 ThenCall MsgBox("Your array has no dimensions. " & _        "It may have just been zapped!", vbExclamation)ElseMsgBox Err.DescriptionEnd IfEnd Sub

This simply displays the LBound and UBound properties of your array. If the array doesn't have an LBound or UBound (ie, you've ZAPPED IT!) then error nine occurs and our lil' message box is displayed.

Well, that's all for this mini project. Don't forget you can download the full source code here.

So, key points we've learned in this section:

  • You can use Erase to wipe clean a dynamic array
  • LBound and UBound cause an error nine if no dimensions exist

Page 7 of 11

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date