Results 1 to 8 of 8
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Array class for working with Arrays

    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.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    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.

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by rpeare View Post
    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"

    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

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    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

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    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

    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
    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.

    EDIT: just remember, arrays are 0 indexed in VBA so your array items start at 0

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by rpeare View Post
    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

    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
    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.

    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????

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    ah, that I do not know.

  8. #8
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    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.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Working on a database design for computer repair class
    By chrscote in forum Database Design
    Replies: 9
    Last Post: 01-25-2015, 11:12 PM
  2. Custom class not working on Enter key
    By Ruegen in forum Programming
    Replies: 1
    Last Post: 10-29-2014, 10:44 PM
  3. sql query arrays
    By drewdrew in forum Programming
    Replies: 3
    Last Post: 07-03-2014, 03:35 AM
  4. Need Help With a Class
    By Alaskanhawg in forum Access
    Replies: 8
    Last Post: 12-14-2013, 05:46 PM
  5. passing arrays io a report
    By Seamus59 in forum Reports
    Replies: 3
    Last Post: 05-21-2013, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums