Has anyone made a custom array class?
That way you could call methods on an array like array.sortAZ() or array.append() and so on
I'm just curious because in other languages this stuff isn't a problem however I am not seeing this in VBA.
Has anyone made a custom array class?
That way you could call methods on an array like array.sortAZ() or array.append() and so on
I'm just curious because in other languages this stuff isn't a problem however I am not seeing this in VBA.
would creating a multi dimensional array serve the purpose or would that, to you, defeat the purpose, i.e. you'd have to loop through the array once to set it's position/order then use it like any other array.
If you're talking about adding values to the array (with the array.append()) you can add items to arrays in VBA.
to add items you'd need to know which one you want to add to the array e.g. myArray[5] = "Value"would creating a multi dimensional array serve the purpose or would that, to you, defeat the purpose, i.e. you'd have to loop through the array once to set it's position/order then use it like any other array.
If you're talking about adding values to the array (with the array.append()) you can add items to arrays in VBA.
whereas array.append would be
myArray.append("Value")
you could have all sorts of methods
myArray.size <-- gives you the amount of held values (good for loops)
myArray.remove(5) <--- removes the 5th value
myArray.insertAfter(4) <-- insert value after 4th value
myArary.destroy <-- clear array completely
myArray.clear <-- clears all values to null however keeps the count
update:
myArray.toIntegers
myArray.toStrings
etc, etc, etc
you'd also have an option to
myArray.build("value 1", "value 2", "value 3")
where the function would loop through the string collecting the values between the quotes, separating them by a comma and then continuing on to the next until it has appended all the values - however you would have to cover for when programmers add quote characters and commas into the string itself.
You could have a private function in the class that does all the appending and call it by it's type and cover for bad values data types or convert them appropriately before appending them. Then the public append method would be just .append and that method would use the private one depending on the data type (you could have intAppend etc)
private strAppend(ValueImport as string)
better yet you could have a subclass of the array so
cArray.integerArray
cArray.stringArray
cArray.longArray
cArray.dateArray
when you create the object etc
I'm not sure where to start!
you can get the number of items in an array with
ubound(myarray)
to remove an array you can use a custom function
I think as long as you declare the array as variant (though I've declared it as string for testing) you should be able to use either text or numbers and 'convert' them at will using a similar function or am I on the wrong track.Code:Sub main()Dim sString Dim myArray() As String sString = "Test 1,Test 2,Test 3" myArray = Split(sString, ",") For i = 0 To UBound(myArray) Debug.Print myArray(i) Next i Call RemoveArrayItem(myArray, 2) For i = 0 To UBound(myArray) Debug.Print myArray(i) Next i End Sub Function RemoveArrayItem(ItemArray As Variant, ByVal iItem As Long) For i = iItem To ltop ItemArray(i) = ItemArray(i + 1) Next i ReDim Preserve ItemArray(LBound(ItemArray) To UBound(ItemArray) - 1) End Function
EDIT: just remember, arrays are 0 indexed in VBA so your array items start at 0
Thanks - I'm pretty familiar with arrays as I work with them from time to time so I have a lot of functions that I could use - however I am wondering if this (making an array class) has already been done before????I'm not sure where to start!
you can get the number of items in an array with
ubound(myarray)
to remove an array you can use a custom function
I think as long as you declare the array as variant (though I've declared it as string for testing) you should be able to use either text or numbers and 'convert' them at will using a similar function or am I on the wrong track.Code:Sub main()Dim sString Dim myArray() As String sString = "Test 1,Test 2,Test 3" myArray = Split(sString, ",") For i = 0 To UBound(myArray) Debug.Print myArray(i) Next i Call RemoveArrayItem(myArray, 2) For i = 0 To UBound(myArray) Debug.Print myArray(i) Next i End Sub Function RemoveArrayItem(ItemArray As Variant, ByVal iItem As Long) For i = iItem To ltop ItemArray(i) = ItemArray(i + 1) Next i ReDim Preserve ItemArray(LBound(ItemArray) To UBound(ItemArray) - 1) End Function
EDIT: just remember, arrays are 0 indexed in VBA so your array items start at 0
ah, that I do not know.
I build class objects in VBA all the time. They are not polymorphic like most other languages and because of that many people don't consider VBA to be a true OOP language. The lack of ability to pass parameters to a constructor is also a huge draw back of classes in VBA. However classes are still highly useful in VBA.
The OP's question is very doable in VBA classes, but it would take some diligence to try to create the functionality he's looking for. Since there is no inheritance in VBA each method of an array would have to be recreated as its own method as part of the class. A private variable could be used to store the values and each time a new value is added or removed you can redim the private variable within the method or property of the class.
I would suggest a collection or dictionary though as they can hold objects and are not tied to one specific data type. In a VBA array you can only use one data type for all of the values stored. It's also very pesky having to redim arrays all the time if you don't know the total number of values to be stored. With dictionaries and collections you can add or remove without having to worry about it. The downside of collections and dictionaries is the byte sizes used compared to arrays. But for today's computers, this is negligible.