About text field instance and variable names

In the Instance Name text box in the Property inspector, you must assign an instance name to a text field to invoke methods and get and set properties on that text field.

In the Var text box in the Property inspector, you can assign a variable name to a dynamic or input text field. You can then assign values to the variable. This is a deprecated functionality that you might use when you create applications for older versions of Flash Player (such as Flash Player 4). When you target newer players, target the text of a text field by using its instance name and ActionScript.

Do not confuse a text field's instance name with its variable name, however. A text field's variable name is a variable reference to the text contained by that text field; it is not a reference to an object.

For example, if you assigned a text field the variable name myTextVar, you can use the following code to set the contents of the text field:

var myTextVar:String = "This is what will appear in the text field";

However, you can't use the variable name myTextVar to set the text field's text property. You have to use the instance name, as shown in the following code:

// This won't work.
myTextVar.text = "A text field variable is not an object reference";

// For input text field with instance name "myField", this will work.
myField.text = "This sets the text property of the myField object";

Use the TextField.text property to control the contents of a text field, unless you're targeting a version of Flash Player that doesn't support the TextField class. This reduces the chances of a variable name conflict, which could result in unexpected behavior at runtime.

For samples that demonstrate how to work with text fields using ActionScript, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download the Samples zip file and navigate to the ActionScript 2.0/TextFields folder to access these samples:


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000881.html