Thursday, 27 September 2018

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

In the 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 Queue's class's method list (included in listing) I could start to play around and write some sample code against the Queue class. Enjoy!

Sample System.Collections.Queue code


'                    Void Clear()
'                  Object Clone()
'                 Boolean Contains(?)
'                    Void CopyTo(?,?)
'                  Object Dequeue()
'                    Void Enqueue(?)
'                 Boolean Equals(?)
'                   Int32 get_Count()
'                 Boolean get_IsSynchronized()
'                  Object get_SyncRoot()
'             IEnumerator GetEnumerator()
'                   Int32 GetHashCode()
'                    Type GetType()
'                  Object Peek()
'                   Queue Synchronized(?)
'                Object[] ToArray()
'                  String ToString()
'                    Void TrimToSize()

Sub Test()
    Dim obj As Object
    Set obj = CreateObject("System.Collections.Queue")
    obj.Enqueue 4
    obj.Enqueue 8
    obj.Enqueue 12
    
    Dim vLoop As Variant
    For Each vLoop In obj
        Debug.Print vLoop
    Next
    
    Debug.Assert obj.Peek = 4
    Debug.Assert obj.Count = 3
    Debug.Assert obj.Dequeue = 4
    Debug.Assert obj.Count = 2
    Debug.Assert obj.Peek = 8
    Debug.Assert obj.Dequeue = 8
    Debug.Assert obj.Dequeue = 12
    Debug.Assert obj.Count = 0
End Sub

No comments:

Post a Comment