«

»

Apr 01 2008

Appending two Arrays

Using common VBScript is impossible to append on array to another. It is possible using the System.Collections.ArrayList object. The AppendRange method adds the elements of an array to the end of the ArrayList.

Dim arrayList, queue

Private Sub PrintValues( ByRef myList, ByVal separator )
   Dim sb
   Set sb = DotNetFactory.CreateInstance( "System.Text.StringBuilder" )
   For Each obj In  myList
      sb.AppendFormat "{0}{1}", separator, obj
   Next
   Print sb.ToString
End Sub

' ** Creates and initializes a new ArrayList.
Set arrayList = CreateObject( "System.Collections.ArrayList" )
arrayList.Add("The")
arrayList.Add("quick")
arrayList.Add("brown")
arrayList.Add("fox")

' ** Creates and initializes a new Queue.
Set queue = CreateObject( "System.Collections.Queue" )
queue.Enqueue("jumped")
queue.Enqueue("over")
queue.Enqueue("the")
queue.Enqueue("lazy")
queue.Enqueue("dog")

' ** Displays the ArrayList and the Queue.
Print "The ArrayList initially contains the following:"
Call PrintValues( arrayList, Space(1) )
Print "The Queue initially contains the following:"
Call PrintValues( queue, Space(1) )

' ** Copies the Queue elements to the end of the ArrayList.
arrayList.AddRange( queue )

' ** Displays the ArrayList.
Print "The ArrayList now contains the following:"
Call PrintValues( arrayList, Space(1) )

 

Comments are closed.