Accessibility

Flex Article

 

An interoperability guide to using Flex and .NET data types


Table of Contents

Comments

Working with the built-in data types: Primitive types

The following sections describe how to work with the built-in data types.

By default, developers get a lot of classes/types from both Flex SDK and the .NET class library. This includes primitive types, strings, dates, collections, and arrays. WebORB uses loose type marshalling rules. As a result, objects and values sent from ActionScript can be adapted to the types required by server-side methods. To understand the benefits of type adaptation, consider the following C# code snippet:

using System;

namespace GettingStarted.Examples
{
 public class EchoService
 {
  public int echoInteger( int intValue )
  {
    return intValue;
  }
 }
}

And compare it to the following VB.NET code:

Imports System

Namespace GettingStarted.Examples
 Public Class EchoService
  Public Function echoInteger(ByVal intValue As Integer) As Integer
    Return intValue
  End Function
 End Class
End Namespace

The echoInteger method accepts and returns an integer value. Due to type adaptation, the client code can use any of the following argument types: int, uint, Number, and String. See the following ActionScript example:

public function invokeEchoService():void
{
 // construct remote object proxy
 var echoIntService:RemoteObject = new RemoteObject( "GenericDestination" );
 // attach 'result' and 'fault' listeners
 echoIntService.echoInteger.addEventListener( ResultEvent.RESULT, gotResult ); 
   echoIntService.echoInteger.addEventListener( FaultEvent.FAULT, gotFault );

 var intValue:int = 21;
 var uintValue:uint = 21;
 var numberValue = new Number( 21 );

 echoIntService.echoInteger( intValue );
 echoIntService.echoInteger( uintValue );
 echoIntService.echoInteger( numberValue );
 // the string argument is parsed as integer
 echoIntService.echoInteger( "21" );
}

public function gotResult( evt:ResultEvent ):void
{
 Alert.show( "got result " + evt.result );
}

public function gotFault( evt:FaultEvent ):void
{
 Alert.show( "got fault " + evt.fault.faultString );
 }

The C# and VB.NET code in the example above uses the integer data type. However, for the same ActionScript code, the argument type in the server code can be other numeric and non-numeric types. Table 1 summarizes the type mappings from ActionScript to .NET.

Table 1. ActionScript to .NET type mappings

Sent ActionScript type .NET argument type Comments

int, uint, Number

bool / Boolean / bool?
byte / Byte / SByte / byte?
char / Char / char?
short / Int16 / UInt16 / short?
int / Int32 / UInt32 / int?
long / Int64 / UInt64 / long?
decimal / Decimal / decimal?
single / Single / single?
double / Double / double?

If the .NET type is nullable (for example, int?) and the incoming value tests as true in Double.IsNaN( asValue ), the server-side argument value will be null.

System.String
System.Text.StringBuilder

System.TimeSpan

Incoming value is treated as milliseconds. The TimeSpan object is created using TimeSpan.FromMilliseconds

Date

DateTime / DateTime?

long / Int64 / UInt64 / long?

Uses the Ticks property of the DateTime object the incoming value is adapted to.

String

System.String
System.Text.StringBuilder

byte[]

Incoming string value is converted to a byte array using the UTF8 encoding.

char / Char

Uses the first character from the incoming string value.

Boolean

Uses Convert.ToBoolean( incomingString ) to create a Boolean value.

IConvertible

If the server-side argument is an instance of IConvertible, the value is created using Convert.ChangeType( incomingString, targetType )

Guid

Creates an instance of Guid with the value sent by client. The incoming value must use a format accepted by the Guid constructor.

Boolean

bool / Boolean / Boolean?

System.String

Uses true or false values.