Tip

Better Way To Handle Arrays

This is a class that makes handling arrays much easier. To use code, paste all
code below into a script library. Then, just type "Use "<your
script library>" " in the options section wherever you need this. I could have
typed a huge description on how to use everything, and how it
works, but I think the best way is to play with the Class. I have tested it
using string and integer data types. It should be fine with
other types as well. I haven't tested it with multi-dimensional arrays, so I'm
not sure how it would handle them. I'm sure you will find it very useful.
Below is a brief description on how to use the class and it's
functions.

* Create a new class of Array:
Dim testArray as New Array
* This must be done first before using any functions below.

To reference values in the array:
testArray.Array(position)

Initialize array with pre-existing array:
Call testArray.Initialize( existingArray )

Get a copy of the Class array in a seperate variable:
Call testArray.ReturnArray( newArray )

Set the upper bound:
testArray.UBounds = x
( where x is an integer )

Get the upper bound:
x = testArray.UBounds
( where x is an integer )

Set the lower bound:
testArray.LBounds = x
( where x is an integer )

Get the lower bound:
x = testArray.LBounds
( where x is an integer )

Get the number of elements in the array:
x = testArray.Count
( where x is an integer )

Add a new value to the array:
testArray.AppendNewValue( "newvalue" )

Get value of a specific element:
value = testArray.GetNthValue( x )
( where x is an integer ), returns value if found, or false if value
wasn't found.

Set value of a specific element:
testArray.SetNthValue( x, "newValue" )
( where x is an integer, and "newValue" is the new value ), returns
true if value was added, or false if unnsuccessful.

Remove a specific element:
testArray.RemoveNthValue( x )
( where x is an integer ), returns true if successfull, false if
unsuccessful.

Remove all duplicate entries within the array:
testArray.RemoveDuplicateEntries

Remove any blank elements from array:
testArray.RemoveSpaces

Sort array either descending or ascending:
testArray.Sort( SortType )
SortType is a boolean/variant - either true or false. True sorts
ascending, false sorts descending.

Find first element that matches a value:
x = testArray.FindFirstOccurence( "value" )
( "value" is the value it searches for ), this returns an integer
representing the position in the array where the first match was found. If
not found, returns -1.

Find next element that matches a value:
x = testArray.FindNextOccurence( z )
( z is the element number it starts the search), this returns an
integer representing the position in the array where the first match was
found. If not found, returns -1. Uses the value passed when
FindFirstOccurence was originally called.

Determine if a value is contained anywhere in the array:
boolean = testArray.IsMember("value")
returns true if found, false if not found

Determine if the array contains any values:
boolean = testArray.IsNothing
returns true if entire array is empty, false if there are values
In Options section of Script Library:
%Include "Lserr.lss"

In Declarations section of script library:
Public Class Array
'**************************************************
'* Introducing a better way to handle arrays
'**************************************************
Public Array() As Variant
Private valueLookingFor As Variant '* used by the GetNextOccurence
function

'********
'* NEW
'********
Sub New
Redim Array(0)
End Sub

'*****************************
'* APPEND NEW VALUE
'*****************************
Sub AppendNewValue( newValue As Variant )
'* Appends new value to end of array
If Ubound( Array ) = 0 And array( Ubound( array ) ) = "" Then
'* Array was probably just initialized, use the first
opening
Array( Ubound( Array ) ) = newValue
Else
Redim Preserve Array ( Ubound( Array ) + 1 )
Array( Ubound( Array ) ) = newValue
End If
End Sub

'***************
'* INITIALIZE
'***************
Sub Initialize( newValues As Variant )
'* takes an existing array and populates Class array
Redim Array(0)
Forall value In newValues
Me.AppendNewValue( value )
End Forall
End Sub

'**********************
'* GET UBOUNDS
'**********************
Property Get UBounds As Integer
On Error Goto ErrHandler
UBounds = Ubound( Array )
Exit Property
errHandler:
If Err = ErrUninitDynArray Then
'* array hasn't been initialized yet, return -1
UBounds = -1
End If
Exit Property
End Property

'*********************
'* GET LBOUNDS
'*********************
Property Get LBounds As Integer
On Error Goto ErrHandler
LBounds = Lbound( Array )
Exit Property
errHandler:
If Err = ErrUninitDynArray Then
'* array hasn't been initialized yet, return -1
LBounds = -1
End If
Exit Property
End Property

'*********************
'* SET UBOUNDS
'*********************
Property Set UBounds As Integer
On Error Goto ErrHandler
tempLBounds = Me.LBounds
If tempLBounds = -1 Then
'* Array hasn't been initialized yet
'* we don't know LBounds, so make it the same as UBounds
Redim Array( UBounds To UBounds )
Elseif tempLBounds > UBounds Then
'* do nothing, leave the array
Else
'* Array has been initialized, redim it
Redim Array( tempLBounds To UBounds )
End If
Exit Property
errHandler:
Exit Property
End Property

'*********************
'* SET LBOUNDS
'*********************
Property Set LBounds As Integer
On Error Goto ErrHandler
tempUBounds = Me.UBounds
Select Case tempUBounds
Case Is = -1
'* Array hasn't been initialized yet
'* we don't know UBounds, so make it the same as UBounds
Redim Array( LBounds To LBounds )
Case Is = 0
'* assume user doesn't care about upper bounds if it's
only 0
Redim Array( LBounds To LBounds )
Case Is < LBounds
'* do nothing, can't have lower bound bigger than upper
bound
Case Else
Redim Array( LBounds To tempUBounds )
End Select
Exit Property
errHandler:
Exit Property
End Property

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.