DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 20.19 Returning Typed Objects from ASP.NET

20.19.1 Problem

You want to return a typed object to Flash from a .NET back end.

20.19.2 Solution

Use an ASObject and set the ASType property to match the name of the ActionScript class as it is registered in Flash.

20.19.3 Discussion

Flash automatically attempts to convert any returned ASObject values into ActionScript datatypes. It tries to find an ActionScript class that is registered within Flash with the same name as the value of the ASObject's ASType property. Therefore, you can return a typed object to a Flash movie from a .NET back end by using an ASObject and setting its ASType property to the name of an ActionScript class as it is registered in the Flash movie. Use the Add( ) method to add properties to the ASObject. Here is an example of a .NET DLL method that returns an ASObject:

public FlashGateway.IO.ASObject getTypedObject(  ) {

  // Create an ASObject.
  FlashGateway.IO.ASObject aso = new FlashGateway.IO.ASObject(  );

  // Add properties to the object.
  aso.Add("a", "eh");
  aso.Add("b", "bee");

  // Set the ASType of the ASObject to match the name 
  // of a registered ActionScript class.
  aso.ASType = "MyClass";

  // Return the ASObject.
  return aso;
}

Here is an example snippet of ActionScript code that handles the ASObject that the preceding code returns:

function MyClass(a, b) {
  this.a = a;
  this.b = b;
}
MyClass.prototype.a;
MyClass.prototype.b;

// Add a method that just writes all the properties to the Output window.
// This example simply illustrates that the ASObject is correctly converted into a
// MyClass object when returned to Flash.
MyClass.prototype.traceProperties = function (  ) {
  for (var prop in this) {
    trace(prop + " = " + this[prop];
  }
};

Object.registerClass("MyClass", MyClass);

// Create a response object.
myResponse = new Object(  );
myResponse.onResult = function (result) {
  // The result parameter is correctly converted to a MyClass object. 
  // You can prove this because the traceProperties(  ) method iss called.
  result.traceProperties(  );
};

// Call the service function.
myNETService.getTypedObject(myResponse);

.NET web services do not allow you to return ASObject data. This is a limitation of .NET web services, and it prevents you from returning typed objects from them.

20.19.4 See Also

Recipe 20.16

    [ Team LiB ] Previous Section Next Section