PDA

View Full Version : Variables that may or may not be there


FlashBulb
02-21-2008, 02:35 PM
I'm importing variables on a name-value string for creating a chart. The string will vary - sometimes certain variables will be there, sometimes not. I thought I'd be able to deal with that via 'if' statements, like so:

if(vars.line_dot)
{
trace("vars.line_dot");
var lseries:LineSeries = new LineSeries();

var lseriesDisplay:Array = vars.line_dot.split(",");
lseries.setStyle("lineStroke", new Stroke(lseriesDisplay[1], lseriesDisplay[0]));
lseries.xField="Year";
lseries.yField="val1";
lseries.displayName = lseriesDisplay[2];
}

Then, if 'line_dot' was within the string (which has been put into the vars URLVariables in this example), then it would be included, and have a Series created.

But, if 'line_dot' is not part of the string, I get an error:

TypeError: Error #1010: A term is undefined and has no properties.
at FlashVarsTest/createChart()[C:\Documents and Settings\michael.steffler\My Documents\Flex Builder 3\Charting1\src\chartData.as:195]
at FlashVarsTest/parseString()[C:\Documents and Settings\michael.steffler\My Documents\Flex Builder 3\Charting1\src\chartData.as:50]
at FlashVarsTest/initVars()[C:\Documents and Settings\michael.steffler\My Documents\Flex Builder 3\Charting1\src\chartData.as:29]
at FlashVarsTest/___FlashVarsTest_Application1_creationComplete()[C:\Documents and Settings\michael.steffler\My Documents\Flex Builder 3\Charting1\src\FlashVarsTest.mxml:2]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\flex_3_beta3\sdk\frameworks\projects\framew ork\src\mx\core\UIComponent.as:9041]
at mx.core::UIComponent/set initialized()[E:\dev\flex_3_beta3\sdk\frameworks\projects\framew ork\src\mx\core\UIComponent.as:1165]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\flex_3_beta3\sdk\frameworks\projects\framew ork\src\mx\managers\LayoutManager.as:696]
at Function/http://adobe.com/AS3/2006/builtin::apply()

How do I deal with variables that may or may not be there when the data comes in??

dr_zeus
02-21-2008, 07:29 PM
This might help:

if(vars.hasOwnProperty("line_dot"))
{
//do stuff
}