Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-18-2011, 07:12 PM   #1
easleyt
Registered User
 
Join Date: Mar 2011
Location: Los Angeles
Posts: 4
Default Problem with if then else

Sorry but this is my 1st attempt @ actionscript and I've yet to find a really good reference and training is weeks away. I'm using Flex Builder 3 and trying to create a percentage based on current month sales vs prior month sales and avoiding zero divide for entities with no prior month sales but it isn't compiling, the relevant code is:

private function formatCMCPct(item:Object, columnataGridColumn) :String {return fmtCMCPct.format(if(item.PMC == 0) {0;}
else {(item.CMC/item.PMC)*100;}) + "%"; }

<mx:NumberFormatter id="fmtCMCPct" precision="0" useThousandsSeparator="true" useNegativeSign="true"/>


When I first tried simply (item.CMC/item.PMC)*100 it worked except for the zero divide rows. The error is as follows:

1083: Syntax error: else is expected
1084: Syntax error: expecting identifier before if
1084: Syntax error: expecting identifier before rightparen
1084: Syntax error: expecting rightparen before leftbrace
1086: Syntax error: expecting semicolon before leftbrace

Thanks!
easleyt is offline   Reply With Quote
Old 03-18-2011, 07:26 PM   #2
xxneon
Super Moderator
 
xxneon's Avatar
 
Join Date: Dec 2006
Location: Amish Country, PA
Posts: 8,570
Send a message via ICQ to xxneon Send a message via AIM to xxneon Send a message via MSN to xxneon Send a message via Skype™ to xxneon
Default

I think you need to break this up a bit.. I don't think you can nest an if statement inside the format() function call like that ..
try it like this.
ActionScript Code:
private function formatCMCPct(item:Object, column:DataGridColumn) :String {     var perc;     if(item.PMC == 0) {         perc = 0;     } else {         perc = (item.CMC/item.PMC)*100;     }     return fmtCMCPct.format(perc) + "%";  }
__________________
Always optimizing...
xxneon is offline   Reply With Quote
Old 03-18-2011, 08:09 PM   #3
easleyt
Registered User
 
Join Date: Mar 2011
Location: Los Angeles
Posts: 4
Default

Perfect, thank you
easleyt is offline   Reply With Quote
Old 03-18-2011, 09:51 PM   #4
audiopro
Learning Fast
 
audiopro's Avatar
 
Join Date: Dec 2009
Location: Morecambe
Posts: 755
Default

Why does an 'if' statement not work inside a function?
First language I have heard if where this happens.
audiopro is offline   Reply With Quote
Old 03-19-2011, 03:59 AM   #5
xxneon
Super Moderator
 
xxneon's Avatar
 
Join Date: Dec 2006
Location: Amish Country, PA
Posts: 8,570
Send a message via ICQ to xxneon Send a message via AIM to xxneon Send a message via MSN to xxneon Send a message via Skype™ to xxneon
Default

i didn't try it but maybe a nested if would work if you strip the ; from the lines inside the if else so that your not confusing the compiler .. normally you don't see a ; inside parens I think it can possibly cause compile errors..
__________________
Always optimizing...
xxneon is offline   Reply With Quote
Old 03-19-2011, 09:00 AM   #6
audiopro
Learning Fast
 
audiopro's Avatar
 
Join Date: Dec 2009
Location: Morecambe
Posts: 755
Default

Sorry, I hadn't noticed the if statement was inside the parens.
audiopro is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:19 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.