The ActionScript programming environment provides two classes representing a
linear collection: Array and mx.collections.ArrayCollections.
Linear collections from ActionScript naturally map to any .NET collection
implementing System.Collections.ICollection,
including ArrayList, SortedList, Stack, and Queue.
Additionally, an array object from the client side can be adapted to an array
of the corresponding type on the server. Array element types are subject to the
same conversion rules shown in the previous section.
Consider this example, written in C#:
using System;
using System.Collections;
namespace GettingStarted.Examples
{
public class EchoArrays
{
public string[] echoArrayList( string[] arr )
{
return arr;
}
public IList echoList( IList list )
{
return list;
}
public ArrayList echoArrayList( ArrayList list )
{
return list;
}
}
}
And consider this example, written in VB.NET:
Imports System Imports System.Collections Namespace GettingStarted.Examples Public Class EchoArrays Public Function echoArrayList(ByVal arr As String()) As String() Return arr End Function Public Function echoList(ByVal list As IList) As IList Return list End Function Public Function echoArrayList(ByVal list As ArrayList) As ArrayList Return list End Function End Class End Namespace
All the methods in the EchoArrays class receive and return a linear collection represented as an array, IList or ArrayList. From the client-side perspective, it makes no
difference which argument type the server method uses. As long as the client
sends an array, WebORB will properly convert it to the right argument type.
Similarly, when a server method returns an array or a list, WebORB serializes
it as an ActionScript array (see the as
Array cast in the gotResult method below).
ActionScript array serialized from WebORB:
private var arrayServiceProxy:RemoteObject;
public function init():void
{
arrayServiceProxy = new RemoteObject( "GenericDestination" );
arrayServiceProxy.source = "GettingStarted.Examples.EchoArrays";
arrayServiceProxy.addEventListener( FaultEvent.FAULT, gotFault );
arrayServiceProxy.addEventListener( ResultEvent.RESULT, gotResult );
var arrayObj = new Array();
arrayObj[ 0 ] = "Dallas";
arrayObj[ 1 ] = "Tokyo";
arrayObj[ 2 ] = "New York";
arrayServiceProxy.echoArray( arrayObj );
arrayServiceProxy.echoArrayList( arrayObj );
arrayServiceProxy.echoList( arrayObj );
}
private function gotResult( evt:ResultEvent ):void
{
var receivedArray:Array = evt.result as Array;
Alert.show( "got result - " + evt.result );
}
private function gotFault( evt:FaultEvent ):void
{
Alert.show( "Server reported an error - " + evt.fault.faultDetail );
}
In addition to traditional arrays, ActionScript supports associative arrays.
Associative arrays from ActionScript map to the .NET System.Collections.IDictionary interface.
Consider this example, written in C#:
using System;
using System.Collections;
namespace GettingStarted.Examples
{
public class EchoDictionaries
{
public IDictionary echoDictionary( IDictionary dict )
{
return dict;
}
public Hashtable echoHashtable( Hashtable hashtable )
{
return hashtable;
}
}
}
And consider this example, written in VB.NET:
Imports System Imports System.Collections Namespace GettingStarted.Examples Public Class EchoDictionaries Public Function echoDictionary(ByVal dict As IDictionary) As IDictionary Return dict End Function Public Function echoHashtable(ByVal hashtable As Hashtable) As Hashtable Return hashtable End Function End Class End Namespace
Both methods in the EchoDictionaries class shown above receive and return a .NET object either in the form of IDictionary or as a Hashtable. The client code does not need
to be aware of the specific argument type the server expects. As long as it knows
that the server method should receive an associative array, it can perform the
invocation; WebORB will handle type adaptation automatically.
For example, the
following ActionScript code invokes both echoDictionary and echoHashtable methods and
passes the same object as the argument. The server automatically handles
conversion of the incoming ActionScript objects to the corresponding argument
types:
private var assocArrayService:RemoteObject;
public function init():void
{
assocArrayService= new RemoteObject( "GenericDestination" );
assocArrayService.source = "GettingStarted.Examples.EchoDictionaries";
assocArrayService.addEventListener( FaultEvent.FAULT, gotFault );
assocArrayService.addEventListener( ResultEvent.RESULT, gotResult );
var assocArray = new Object();
assocArray[ "USA" ] = "Washington, DC";
assocArray[ "Spain" ] = "Madrid";
assocArray[ "Japan" ] = "Tokyo";
assocArrayService.echoArray( assocArray );
assocArrayService.echoArrayList( assocArray );
}
private function gotResult( evt:ResultEvent ):void
{
var receivedMap:Object = evt.result;
Alert.show( "got result - " + receivedMap[ "USA" ] + " " +
receivedMap[ "Spain" ] + " " +
receivedMap[ "Japan" ] );
}
private function gotFault( evt:FaultEvent ):void
{
Alert.show( "Server reported an error - " + evt.fault.faultDetail );
}