Thursday 27 September 2018

VBA - .NET Interop - System.Collections.ArrayList sample code

In a previous post I showed how to call .NET reflection from VBA to get a list of methods of the .NET System.Collections.* classes. I wanted the method list because I felt deprived without the VBA Intellisense. Once I had the ArrayList's class's method list (included in listing) I could start to play around and write some sample code against the ArrayList class. Enjoy!

Sample System.Collections.ArrayList code

'               ArrayList Adapter(?)
'                   Int32 Add(?)
'                    Void AddRange(?)
'                   Int32 BinarySearch(?)
'                   Int32 BinarySearch(?,?)
'                   Int32 BinarySearch(?,?,?,?)
'                    Void Clear()
'                  Object Clone()
'                 Boolean Contains(?)
'                    Void CopyTo(?)
'                    Void CopyTo(?,?)
'                    Void CopyTo(?,?,?,?)
'                 Boolean Equals(?)
'                   IList FixedSize(?)
'               ArrayList FixedSize_2(?)
'                   Int32 get_Capacity()
'                   Int32 get_Count()
'                 Boolean get_IsFixedSize()
'                 Boolean get_IsReadOnly()
'                 Boolean get_IsSynchronized()
'                  Object get_Item(?)
'                  Object get_SyncRoot()
'             IEnumerator GetEnumerator()
'             IEnumerator GetEnumerator(?,?)
'                   Int32 GetHashCode()
'               ArrayList GetRange(?,?)
'                    Type GetType()
'                   Int32 IndexOf(?)
'                   Int32 IndexOf(?,?)
'                   Int32 IndexOf(?,?,?)
'                    Void Insert(?,?)
'                    Void InsertRange(?,?)
'                   Int32 LastIndexOf(?)
'                   Int32 LastIndexOf(?,?)
'                   Int32 LastIndexOf(?,?,?)
'                   IList ReadOnly(?)
'               ArrayList ReadOnly_2(?)
'                    Void Remove(?)
'                    Void RemoveAt(?)
'                    Void RemoveRange(?,?)
'               ArrayList Repeat(?,?)
'                    Void Reverse()
'                    Void Reverse(?,?)
'                    Void set_Capacity(?)
'                    Void set_Item(?,?)
'                    Void SetRange(?,?)
'                    Void Sort()
'                    Void Sort(?)
'                    Void Sort(?,?,?)
'                   IList Synchronized(?)
'               ArrayList Synchronized_2(?)
'                Object[] ToArray()
'                   Array ToArray(?)
'                  String ToString()
'                    Void TrimToSize()

Sub Test()
    Dim obj As Object
    Set obj = CreateObject("System.Collections.ArrayList")
    obj.Add 20
    obj.Add 16
    obj.Add 12
    obj.Add 8
    obj.Add 4
    Debug.Assert obj.Count() = 5
    Debug.Assert obj.Item(2) = 12
    
    '*
    '* try finding a value in the ArrayList,
    '* we need to call the two argument overload (the one argument version errors)
    '*
    Debug.Assert obj.IndexOf(12, 0) = 2
    
    '*
    '* we can sort the ArrayList
    '*
    obj.Sort
    Debug.Assert obj.Item(0) = 4
    Debug.Assert obj.Item(4) = 20
    
    '*
    '* we can remove an item (cool)
    '*
    Debug.Assert obj.Item(1) = 8
    Debug.Assert obj.Count = 5
    obj.RemoveAt 1
    Debug.Assert obj.Item(1) = 12
    Debug.Assert obj.Count = 4
    
    '*
    '* we can reverse the ArrayList
    '*
    obj.Reverse
    Debug.Assert obj.Item(3) = 4
    Debug.Assert obj.Item(0) = 20
    
    '*
    '* converting to an ordinary array
    '*
    Dim oArray() As Variant
    oArray() = obj.ToArray()
    Debug.Assert oArray(3) = 4
    
End Sub

No comments:

Post a Comment