ternary-operator

Is there a way to use a ternary operator - or similar method - for picking the variable to assign to?

假装没事ソ 提交于 2021-01-27 02:34:10
问题 is it possible to differ the variable I'm assigning to depending on a condition? The issue I came across is wanting to do this: (bEquipAsSecondary ? currentWeaponOffhand : currentWeaponMainhand) = weaponToSwitchTo; Instead of if (bEquipAsSecondary) { currentWeaponOffhand = weaponToSwitchTo; } else { currentWeaponMainhand = weaponToSwitchTo; } Which results in the following error Error CS0131 The left-hand side of an assignment must be a variable, property or indexer So I was wondering if

Inline PHP / HTML Ternary If

安稳与你 提交于 2021-01-21 08:22:20
问题 I am trying to do the following: <li <?PHP ($this->pageName == 'index' ? ?>class="current"<?PHP : '')?>><a href="">Home</a></li> But it is not working. Is what I'm trying to achieve possible? If so what am I doing wrong? I know it's not hard to put PHP in HTML lol. I was curious if a ternary operator could be used in a way that is similar to: <?PHP if(1 == 1){?> <p>Test</p> <?PHP }?> 回答1: What you have done will only RETURN the value "class='current'". You still will be required to ECHO/PRINT

Ternary Operator Inside PHP String

醉酒当歌 提交于 2020-08-24 05:34:05
问题 I want to evaluate a simple ternary operator inside of a string and can't seem to find the correct syntax. My code looks like this: foreach ($this->team_bumpbox as $index=>$member) echo ".... class='{((1) ? abc : def)}'>...."; but I can't seem to get it to work properly. Any ideas on how to implement this? 回答1: You can't do it inside the string, per se. You need to dot-concatenate. Something like this: echo ".... class='" . (1 ? "abc" : "def") . "'>...."; 来源: https://stackoverflow.com

Why can braced-init-list not be used in ternary operator?

余生颓废 提交于 2020-07-28 08:48:47
问题 My compiler is the latest VC++ 2013 RC. int f(bool b) { return {}; // OK return b ? 1 : { }; // C2059: syntax error : '{' return b ? 1 : {0}; // C2059: syntax error : '{' return b ? {1} : {0}; // C2059: syntax error : '{' } Why can braced-init-list not be used in ternary operator? Is this behavior defined as ill-formed by the C++ standard, or just a bug of the VC++ compiler? 回答1: Well, here's what the standard says about the braced-init-list (8.5.3.1): List-initialization can be used as the

Ternary operator implicit cast to base class

霸气de小男生 提交于 2020-07-28 06:33:06
问题 Consider this piece of code: struct Base { int x; }; struct Bar : Base { int y; }; struct Foo : Base { int z; }; Bar* bar = new Bar; Foo* foo = new Foo; Base* returnBase() { Base* obj = !bar ? foo : bar; return obj; } int main() { returnBase(); return 0; } This doesn't work under Clang or GCC, giving me : error: conditional expression between distinct pointer types ‘Foo*’ and ‘Bar*’ lacks a cast Base* obj = !bar ? foo : bar; Which means for it to compile I have to change the code to : Base*

Why unset() doesn't work in PHP ternary operator

谁都会走 提交于 2020-07-19 05:05:52
问题 So there is a problem with this, but i'm blind to it. Even after reading the documentation twice (PHP Comparison Operators) isset($items['blog']) ? unset($items['blog']) : NULL; Parse error: syntax error, unexpected T_UNSET 回答1: A @Bryan points out, no function calls to language constructs within the ternary operator. Since there's no return value involved at all here though, just do: unset($items['blog']); There's no need to check if the value is set or not beforehand. If it is not, unset

How to use a Ternary Operator with multiple condition in flutter dart?

一笑奈何 提交于 2020-07-17 05:48:09
问题 how to use ternary if else with two or more condition using "OR" and "AND" like if(foo == 1 || foo == 2) { do something } { else do something } i want to use it like foo == 1 || foo == 2 ? doSomething : doSomething 回答1: If you're referring to else if statements in dart, then this ternary operator: (foo==1)?something1():(foo==2)? something2():(foo==3)? something3(): something4(); is equivalent to this: if(foo ==1){ something1(); } elseif(foo ==2){ something2(); } elseif(foo ==3){ something3();

How to use a Ternary Operator with multiple condition in flutter dart?

两盒软妹~` 提交于 2020-07-17 05:45:08
问题 how to use ternary if else with two or more condition using "OR" and "AND" like if(foo == 1 || foo == 2) { do something } { else do something } i want to use it like foo == 1 || foo == 2 ? doSomething : doSomething 回答1: If you're referring to else if statements in dart, then this ternary operator: (foo==1)?something1():(foo==2)? something2():(foo==3)? something3(): something4(); is equivalent to this: if(foo ==1){ something1(); } elseif(foo ==2){ something2(); } elseif(foo ==3){ something3();

How to use a Ternary Operator with multiple condition in flutter dart?

故事扮演 提交于 2020-07-17 05:44:08
问题 how to use ternary if else with two or more condition using "OR" and "AND" like if(foo == 1 || foo == 2) { do something } { else do something } i want to use it like foo == 1 || foo == 2 ? doSomething : doSomething 回答1: If you're referring to else if statements in dart, then this ternary operator: (foo==1)?something1():(foo==2)? something2():(foo==3)? something3(): something4(); is equivalent to this: if(foo ==1){ something1(); } elseif(foo ==2){ something2(); } elseif(foo ==3){ something3();