?
Solved

How to check if a var exists in AS3

Posted on 2009-12-18
3
Medium Priority
?
806 Views
Last Modified: 2013-11-11
I use a "for" function to loop thru all my vars, when flash detects that the var does not exist it throws an error:

ReferenceError: Error #1069: Property cbTipo0txt not found on deca.pwd.v4.LogPerUnit and there is no default value.
      at deca.pwd.v4::LogPerUnit/setAsDefault()

How can I check if a var exists without the flash error?

I tried the try-catch but when encounters an error the loop is truncated and starts with the next value of "i" losing the values ahead.


var AuxDefault:Array = new Array();
			
			for (var i:Number = 0; i < 14; i++) 
			{	
				var AuxDefaultS:String = "";
				trace("i1:", i);
				
				if ( i < 14) 
				{
					AuxDefaultS = i+"|"+this["cbTipo"+i].selected;
				}
				
				if (this["cbTipo"+i+"cp"]) 
				{
					AuxDefaultS += "||cp|"+this["cbTipo"+i+"cp"].selectedColor;
					trace("cbTipo"+i+"cp");
				}

				if (this["cbTipo"+i+"txt"]) 
				{
					AuxDefaultS += "||txt|"+this["cbTipo"+i+"txt"].text;
				}
				
				if (this["cbTipo"+i+"ns"]) 
				{
					AuxDefaultS += "||ns|"+uiRange(this["cbTipo"+i+"ns"]).value;
				}
				
				if (this["cbTipo"+i+"rb0"]) 
				{
					AuxDefaultS += "||rb0|"+this["cbTipo"+i+"rb0"].selected;
				}
				
				if (this["cbTipo"+i+"rb1"]) 
				{
					AuxDefaultS += "||rb1|"+this["cbTipo"+i+"rb1"].selected;
				}
				
				if (this["cbTipo"+i+"rb2"]) 
				{
					AuxDefaultS += "||rb2|"+this["cbTipo"+i+"rb2"].selected;
				}
				
				AuxDefault.push(AuxDefaultS);
			}
			
			var obj:Object = {DEFAULTS:AuxDefault.join("|||")};
			trace ("setAsDefault:", obj.DEFAULTS);

Open in new window

0
Comment
Question by:grupodeca
  • 2
2 Comments
 
LVL 14

Accepted Solution

by:
TanLiHao earned 1000 total points
ID: 26086035
This is how you check whether a variable is defined in AS3.

var hello:String = "HEHE";
var bye:Number = 5;

var varsToTest:Vector.<String> = Vector.<String>(["hello", "bye", "NonExistingVar", "varsToTest"]);


for (var i:int = 0; i < varsToTest.length; i++) {
    if(this.hasOwnProperty(varsToTest[i])) {
        trace(varsToTest[i] + " is defined.");
    } else {
        trace(varsToTest[i] + " is undefined.");
    }
}

This is some example code I wrote out, it is not relevant to what you show me but you should get the idea. The idea is using hasOwnProperty to check whether the variable exists.
0
 
LVL 14

Expert Comment

by:TanLiHao
ID: 26086048
Actually I am not modifying your code for you because I do not understand your code entirely and I believe it can be optimised and become shorter. I also wonder why you need to check whether a variable exists or not at runtime.


Anyhow, I modified the code of yours using the hasOwnProperty method without changing much code, this is because of the reason I mentioned above. Therefore you should modify the code yourself.

var AuxDefault:Array = new Array();
                        
                        for (var i:Number = 0; i < 14; i++) 
                        {       
                                var AuxDefaultS:String = "";
                                trace("i1:", i);
                                
                                if ( i < 14) 
                                {
                                        AuxDefaultS = i+"|"+this["cbTipo"+i].selected;
                                }
                                
                                if (this.hasOwnProperty("cbTipo" + i + "cp")) 
                                {
                                        AuxDefaultS += "||cp|"+this["cbTipo"+i+"cp"].selectedColor;
                                        trace("cbTipo"+i+"cp");
                                }

                                if (this.hasOwnProperty("cbTipo" + i + "txt")) 
                                {
                                        AuxDefaultS += "||txt|"+this["cbTipo"+i+"txt"].text;
                                }
                                
                                if (this.hasOwnProperty("cbTipo" + i + "ns")) 
                                {
                                        AuxDefaultS += "||ns|"+uiRange(this["cbTipo"+i+"ns"]).value;
                                }
                                
                                if (this.hasOwnProperty("cbTipo" + i + "rb0")) 
                                {
                                        AuxDefaultS += "||rb0|"+this["cbTipo"+i+"rb0"].selected;
                                }
                                
                                if (this.hasOwnProperty("cbTipo" + i + "rb1")) 
                                {
                                        AuxDefaultS += "||rb1|"+this["cbTipo"+i+"rb1"].selected;
                                }
                                
                                if (this.hasOwnProperty("cbTipo" + i + "rb2")) 
                                {
                                        AuxDefaultS += "||rb2|"+this["cbTipo"+i+"rb2"].selected;
                                }
                                
                                AuxDefault.push(AuxDefaultS);
                        }
                        
                        var obj:Object = {DEFAULTS:AuxDefault.join("|||")};
                        trace ("setAsDefault:", obj.DEFAULTS);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question