User Guide

Example: PlayList 253
Combining array elements into a character-delimited
string
In addition to using an array to maintain the song list in the PlayList class, in this example
arrays are also used in the Song class to help manage the list of genres to which a given song
belongs. Consider this snippet from the Song classs definition:
private var _genres:String;
public function Song(title:String, artist:String, year:uint,
filename:String, genres:Array)
{
...
// Genres are passed in as an array
// but stored as a semicolon-separated string.
this._genres = genres.join(";");
}
When creating a new Song instance, the genres parameter that is used to specify the genre
(or genres) the song belongs to is defined as an Array instance. This makes it convenient to
group multiple genres together into a single variable that can be passed to the constructor.
However, internally the Song class maintains the genres in the private
_genres variable as a
semicolon-separated String instance. The Array parameter is converted into a semicolon-
separated string by calling its
join() method with the literal string value ";" as the specified
delimiter.
By the same token, the
genres accessors allow genres to be set or retrieved as an Array:
public function get genres():Array
{
// Genres are stored as a semicolon-separated String,
// so they need to be transformed into an Array to pass them back out.
return this._genres.split(";");
}
public function set genres(value:Array):void
{
// Genres are passed in as an array,
// but stored as a semicolon-separated string.
this._genres = value.join(";");
}
The genres set accessor behaves exactly the same as the constructor; it accepts an Array and
calls the
join() method to convert it to a semicolon-separated String. The get accessor
performs the opposite operation: the
_genres variable’s split() method is called, splitting
the String into an array of values using the specified delimiter (the literal string value
";" as
before).