Avg. Rating 5.0
Tags:



Problem

In some situations we need to be sure whether the correct type of data has been passed within an array list for an argument of a method or has been received from a specific function.

Solution

Using the Vector class we can specify which type of data will be accepted in our list, allowing the insertion if only the type required was matched.

Detailed explanation

Ok guys,
 
To explain the usage of Vector class, let's imagine the following situation:
 
You have a custom class called User. And another class must retrieve a list of User objects, for some purpose. Eg: To fill text input objects with its properties, or fill a datagrid. For this reason, you need to be sure if the correct type of data - in this case "USER" - was pushed to your array list, to avoid errors during runtime.
 
Creating a Vector typed as USER list, we say to ActionScript that our list will only receive this type of data, and no other type will be allowed. This way, we're sure the right type will be passed, cause when we compile our app, an error will be automatically dispatched advising us that we are trying to use a wrong type in our list. So, we can get back to our source code and do modifications as needed.
 
Going ahead, if other developer use your class, you don't need to be worried if it will work as expected, because the code itself will notify if an error occur.
 
Try this:
 
1. Create a new ActionScript 3.0 file and type the code below:
        package {

public class User {

public var firstname:String = null;
public var lastname:String = null;
public var email:String = null;

public function User():void { }

}

}
 
  

2. Create a Flash File (ActionScript 3.0) within the same directory of our ActionScript class, and then type the following code:

        var mylist:Vector.<User> = new Vector.<User>();
try {
mylist.push("Hello");
} catch(e:Error) {
trace(e.message);
} 
 
 
  

Probably you will get an error message, right? Why? Cause you're trying to insert a String in our list, not a User object.

However, if you try the following:

        var mylist:Vector.<User> = new Vector.<User>();
try {
var user:User = new User();
user.firstname = "Jhonatan"; user.lastname = "Rosa Jacinto"; user.email = "foo@foo.com";
mylist.push(user);
trace(mylist[0].firstname);
}
catch(e:Error) {
trace(e.message);
} 
 
  

You will get "JHONATAN" in the output panel... because the object inserted was an User Object.
Very very simple, right?

One thing... if I had the following code:

        var list:Vector.<DisplayObject> = new Vector.<DisplayObject>();
list.push(new MovieClip()); 
 
 
  

This code will generate an error or not?

The answer is NOO! It will work good! Why? The reason is a MovieClip object is an object of type DisplayObject, cause the MovieClip class extends from the DisplayObject class... so any MovieClip object is a DisplayObject as well.

Following this thinking, the code below will not work:

        var list:Vector.<DisplayObject> = new Vector.<DisplayObject>();
list.push(new User());
 
  

Why? As explained before, our User class doesn't extend from DisplayObject or MovieClip class. So, it cannot be pushed into our list.

I hope you enjoy this tutorial.
Thanks guys and see you next time! ;)


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes