16.10. Adding Data to a Server-Side Shared Object

Problem

You want to add values to a server-side shared object.

Solution

Use the SharedObject.setProperty( ) method.

Discussion

If you try to add values to a server-side shared object in the same way that you add values to a client-side shared object, you will receive an error. The correct way to add values (or modify existing values) to a server-side shared object is to use the SharedObject.setProperty( ) method. The setProperty( ) method requires that you specify both the name of the property and the value to assign to the property.

// This example code should appear in an .asc file. It adds a property named
// myProperty to a shared object and assigns the property the value of 6.
my_r_so.setProperty("myProperty", 6);

If you want to modify an existing property relative to its current value, you can use the getProperty( ) method in conjunction with setProperty( ):

// Set the myProperty property to one more than its current value.
my_r_so.setProperty("myProperty", my_r_so.getProperty("myProperty") + 1);

See Also

See Recipe 16.9 for details on SharedObject.getProperty( ). See Recipe 16.2 for important differences when setting data for a local shared object.

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.