3

How i write the inline conditional statement in Flex with two expressions(case)

like

text="{expression, expression2 ? true:false}"

Flex compiler only check the first expression and on behalf of that give result.But i want to check both statement and show result. if no condition met then do nothing.

Tahir Alvi
  • 896
  • 2
  • 14
  • 44
  • 3
    Probably something like `text = ((expression) && (expression2)) ? true : false;` or `text = ((expression) || (expression2)) ? true : false;`. – Taurayi Jan 07 '12 at 14:32

4 Answers4

11

If I understand your question correctly either:

text = ((expression) && (expression2)) ? true : false;

or text = ((expression) || (expression2)) ? true : false;

[UPDATE 1]

Well I say either, but the first tests to see if both conditions are true, where as the second tests if either condition is true which I believe is the one you want.

[UPDATE 2]

An example

((1.1 is int) && (1.1 is Number)) ? true : false;

This will give you false as the expression (1.1 is int) is false, and both expressions must be true to return true.

((1.1 is int) || (1.1 is Number)) ? true : false;

This will return true as the expression (1.1 is Number) is true, and only one expression has to be true to return true.

[FINAL UPDATE]

Last example:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:VGroup>
        <s:Label id="label1" text="{((1.1 is int) &amp;&amp; (1.1 is Number)) ? 'true' : 'false'}"></s:Label>
        <s:Label id="label2" text="{((1.1 is int) || (1.1 is Number)) ? 'true' : 'false'}"></s:Label>
    </s:VGroup>
</s:Application> 

Because you get an error if you use && alternatively you can use &amp;&amp;.

Taurayi
  • 3,209
  • 2
  • 17
  • 15
  • @wvxw I'm not sure what you mean, if you were the one that downvoted my answer, can you explain to me how my answer was wrong. My answer might be wrong but I don't understand what your trying to say in your comment in relation to my answer be wrong. – Taurayi Jan 07 '12 at 16:56
  • in simple word i have data in three condition – Tahir Alvi Jan 08 '12 at 07:37
  • 1st - true, 2nd - false and 3rd is not true not false so i point out that if i only implement two condition in my question expression and expression 2. Mean if expression is matched then text populate according to that and if expression 2 is matched text populate according to that but for now it only check the 1st condition and on behalf of that populate result. – Tahir Alvi Jan 08 '12 at 07:40
  • 1
    @wvxw its still not clear to me at all what you mean, 2 other people answered more or less the same as me increasing the odds that my answer is correct. Also my comment to this question (at the top of the page, which is more or less the same as my answer) as well as my answer were both voted up, therefore (to me) further affirms that my answer is correct(since other seem to agree). Don't get me wrong, it's not like I'm saying that my answer is wrong, just that I do not understand how exactly it is wrong. Can you please explain clearly? – Taurayi Jan 08 '12 at 08:13
  • @Tahir Alvi I thought your question was pretty clear but apparently it's not, can you please update your question with the current code your using, that way I can try and deduce what exactly your problem is via your code. – Taurayi Jan 08 '12 at 08:14
  • 2
    oh, ok, I'm pretty sure your talking about something that has nothing to do with my answer, therefore your downvote wasn't justified, thats all I wanted to know. – Taurayi Jan 09 '12 at 01:57
5

There are number of methods, you can just write like that :

(condition1 && condition2) ? statement if true : statement if false

but if you want to execute this code in mxml component bindings, then you must use :

'&amp &amp' instead of &&,'&lt' instead of <, etc.

rdurand
  • 7,342
  • 3
  • 39
  • 72
Kamran Aslam
  • 123
  • 2
  • 9
0

I would write the following statements instead :

var expression1:Boolean;
var expression2:Boolean;
var result:Boolean;

expression1 = condition?true:false;
expression2 = anotherCondition?true:false;

result = expression1&&expression2;
Zakaria
  • 14,892
  • 22
  • 84
  • 125
0

If you want them in one if statement, use the and or or operators: &&~ and||`, respectively. For example:

var x:int = 6
if (x > 3 && x < 10) {
    trace("both are true, so its contents will be acted on.");
}

var bob:Boolean = true;
if (x > 10 || bob) {
    trace("x > 10 is false, but bob is true, so this will also be acted on.");
}

You can also use functions that return booleans

function y (z:int):Boolean {
    if (z < 10) {
        return false;
    } else {
        return true;
    }
}

Actually, I could simplify that to

function y (z:int) {
    return z >= 10;
}

Then use the function in a group of conditions:

if (x < 5 && y(x)) { trace ("something here"); }

Booleans are converted to strings if assigned to or concatenated with a String.

Mar
  • 7,765
  • 9
  • 48
  • 82