October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Discovering Arrays

  • November 19, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

If you fancy becoming an array wizard, there are a couple of other little tips and tricks you might be interested in.

Array Function

The Array function can turn a regular variant variable into an array. Let's peek at an example:

Dim MyVariable As VariantMyVariable = Array("These", "Are", "Elements", _                   "Of", "My", "Array")For i = LBound(MyVariable) To UBound(MyVariable)MsgBox MyVariable(i)Next i

Try running this example. The code takes a variant variable and turns it into an array. It's just a cunningly quick way to create an array.

IsArray Function

So, if regular variables can be turned into arrays how do you know whether something is an array or isn't? Oh boy.

Thankfully the IsArray function can help out. Just call IsArray, passing the possible array. It will return a True or False value.

Here's a little sample code:

Dim RegularArray(1 To 5) As StringDim DynamicArray() As StringDim ToBeChangedVariable As VariantDim RegularVariable As VariantToBeChangedVariable = Array(1, 2, 3)MsgBox ("RegularArray - " & IsArray(RegularArray))MsgBox ("DynamicArray - " & IsArray(DynamicArray))MsgBox ("ToBeChangedVariable - " & IsArray(ToBeChangedVariable))MsgBox ("RegularVariable - " & IsArray(RegularVariable))

IsArray returns True for both RegularArray and DynamicArray. After we use the Array function on ToBeChangedVariable, it turns into an array meaning IsArray returns True there also.

But the final RegularVariable check returns a False. That isn't an array.

IsZapped Function

OK, so there isn't an intrinsic Visual Basic function called IsZapped. It's my own creation and can be incredibly useful.

Instead of adding error handling to your code just in case an array has been 'zapped' and left dimensionless, why not just use this groovy little IsZapped function.

If everything is A-OK, it returns a False it ain't zapped. If the array has no dimensions, it returns a True.

Here's the code:

Public Function IsZapped(ArrayIn As Variant) As BooleanOn Error GoTo DimensionErrDim i As Integeri = LBound(ArrayIn)IsZapped = FalseExit Function    DimensionErr:    If Err.Number = 9 ThenIsZapped = TrueElseMsgBox Err.DescriptionEnd If    End Function




Page 8 of 11



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel