NexusFi: Find Your Edge


Home Menu

 





Substitution for booleans in C#?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Al2010 with 4 posts (0 thanks)
    2. looks_two gomi with 2 posts (2 thanks)
    3. looks_3 BDawg with 2 posts (3 thanks)
    4. looks_4 traderwerks with 1 posts (1 thanks)
    1. trending_up 1,837 views
    2. thumb_up 6 thanks given
    3. group 4 followers
    1. forum 9 posts
    2. attach_file 0 attachments




 
Search this Thread

Substitution for booleans in C#?

  #1 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10

Hi,
Is there a substitution for booleans, e.g. if bool1 false then return bool2... If there's more then two boolean vals. then check which one is true and return it, e.g. check bool1, ...2, ..3,... bool n - true - then return true.

Looking for similar functions that used in SQL:

NVL(string, replace_string) - if string is false then return replace string.

or

COALESCE(expression1,...n) - equivalent to the following CASE expression:

COALESCE(expression1,...n)
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
WHEN (expression2 IS NOT NULL) THEN expression2
...
ELSE expressionN
END

Is there such thing in C# in general that can be used with numbers and strings? If there is can this be used for boolean expressions/values?
Thank you very much.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Futures True Range Report
The Elite Circle
Better Renko Gaps
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
Battlestations: Show us your trading desks!
26 thanks
The Program
18 thanks
  #3 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505


there is the ? operator
(condition)?val1:val2;

example
string message=(a<b)?"a smaller":"b smaller";

Reply With Quote
Thanked by:
  #4 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465

Try a switch statement.

Reply With Quote
Thanked by:
  #5 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


gomi View Post
there is the ? operator
(condition)?val1:val2;

example
string message=(a<b)?"a smaller":"b smaller";


Hi,
Thank you for your reply. But I could not understand the example. I was looking for if a is null or 0 (zero) then return b. Can you please make another example with exact syntax? How would I actually write in my program if a = 0 then return b or if string a is " " (blank) then return string b? Can this be used with booleans?
Thank you.

Reply With Quote
  #6 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


traderwerks View Post
Try a switch statement.


Thank you for your reply. The first question, is there a single line command/method/statement such as Coalesce (expr1,...2....3....n) in C# or I must use switch statement in such cases? Another question, can switch be used with booleans? If not then what can be used with boolean expressions?
Thank you very much.

Reply With Quote
  #7 (permalink)
 
BDawg's Avatar
 BDawg 
Seattle, WA
 
Experience: Advanced
Platform: Tradestation, C#
Broker: Tradestation,TD,Tradier
Trading: Options, ETFs, VIX, Indices
Posts: 15 since Oct 2010
Thanks Given: 64
Thanks Received: 11


Al2010 View Post
Hi,
Thank you for your reply. But I could not understand the example. I was looking for if a is null or 0 (zero) then return b. Can you please make another example with exact syntax? How would I actually write in my program if a = 0 then return b or if string a is " " (blank) then return string b? Can this be used with booleans?
Thank you.

 
Code
//
// condition ? resultIfTrue : resultIfFalse 
// This is is a ternary (aka 3 way) operator that tests condition on the left of the question mark, then returns
// the value to the left of the colon ":" if the condition is true, or returns the value to the right of the colon if the
// condition is false.
 
bool Foo(bool condition1, bool condition2, bool condition3, bool condition4)
{
     return (condition1 ? condition1 : (condition2 ? condition2 : (condition3 ? condition3 : condition4)));
}

 
Code
int Foo2(int& a, int b)
{
     return (a.HasValue)  ? a : b);
}
 
Code
bool Foo2(string a, string b)
{
     return (string.IsNullOrEmpty(a) || string.IsWhiteSpace(a) ? b : a);
}
There is also a new ?? operator which returns a if a is not null and b if it is.
 
Code
bool Foo2(string a, string b)
{
     return a ?? b;
}
Does that make sense?

HTH-

Reply With Quote
Thanked by:
  #8 (permalink)
 gomi 
Paris
Market Wizard
 
Experience: None
Platform: NinjaTrader
Posts: 1,270 since Oct 2009
Thanks Given: 282
Thanks Received: 4,505


BDawg View Post
 
Code
bool Foo2(int a, int b)
{
     return (a == null)  ? b : a);
}

I don't think you can test int for null if they're not nullable, that's the int? type. In that case it's easier to use ?? operator :

?? Operator (C#)

 
Code
                            
        // ?? operator example.
        
intnull;

        
// y = x, unless x is null, in which case y = -1.
        
int y ?? -1

Reply With Quote
Thanked by:
  #9 (permalink)
 
BDawg's Avatar
 BDawg 
Seattle, WA
 
Experience: Advanced
Platform: Tradestation, C#
Broker: Tradestation,TD,Tradier
Trading: Options, ETFs, VIX, Indices
Posts: 15 since Oct 2010
Thanks Given: 64
Thanks Received: 11


Quoting 
I don't think you can test int for null if they're not nullable, that's the int? type. In that case it's easier to use ?? operator :

D'oh! Yes, of course you are right. Fixed in the other post.
Sorry for the confusion that might have caused.

Reply With Quote
Thanked by:
  #10 (permalink)
Al2010
 
Posts: 50 since Oct 2010
Thanks Given: 36
Thanks Received: 10


Thank you very much to all for helping me. You guys rock!

Reply With Quote




Last Updated on November 29, 2010


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts