Substitution for booleans in C#? - futures io
futures io



Substitution for booleans in C#?


Discussion in NinjaTrader

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

Substitution for booleans in C#?

(login for full post details)
  #1 (permalink)
Al2010
 
 
Posts: 50 since Oct 2010
Thanks: 36 given, 10 received

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 futures io?
TradingView help in improving Anchored Vwap
Platforms and Indicators
1 min chart v-shape scan help please
ThinkOrSwim
For the experts of Metastock
Platforms and Indicators
Is there inherently more opportunity in smaller markets?
Traders Hideout
Eurex Denali vs dxFeed
Sierra Chart
 
 
(login for full post details)
  #3 (permalink)
 gomi 
Paris
 
Experience: None
Platform: NinjaTrader
 
Posts: 1,250 since Oct 2009
Thanks: 275 given, 4,430 received


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

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

Reply With Quote
The following user says Thank You to gomi for this post:
 
(login for full post details)
  #4 (permalink)
 traderwerks   is a Vendor
 
 
Posts: 693 since Jun 2009
Thanks: 435 given, 464 received

Try a switch statement.

Reply With Quote
The following user says Thank You to traderwerks for this post:
 
(login for full post details)
  #5 (permalink)
Al2010
 
 
Posts: 50 since Oct 2010
Thanks: 36 given, 10 received


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
 
(login for full post details)
  #6 (permalink)
Al2010
 
 
Posts: 50 since Oct 2010
Thanks: 36 given, 10 received


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
 
(login for full post details)
  #7 (permalink)
 BDawg 
Seattle, WA
 
Experience: Advanced
Platform: Tradestation, C#
Broker: Tradestation,TD,Tradier
Trading: Options, ETFs, VIX, Indices
 
BDawg's Avatar
 
Posts: 15 since Oct 2010
Thanks: 64 given, 11 received


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
The following 2 users say Thank You to BDawg for this post:
 
(login for full post details)
  #8 (permalink)
 gomi 
Paris
 
Experience: None
Platform: NinjaTrader
 
Posts: 1,250 since Oct 2009
Thanks: 275 given, 4,430 received


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
The following user says Thank You to gomi for this post:
 
(login for full post details)
  #9 (permalink)
 BDawg 
Seattle, WA
 
Experience: Advanced
Platform: Tradestation, C#
Broker: Tradestation,TD,Tradier
Trading: Options, ETFs, VIX, Indices
 
BDawg's Avatar
 
Posts: 15 since Oct 2010
Thanks: 64 given, 11 received


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
The following user says Thank You to BDawg for this post:
 
(login for full post details)
  #10 (permalink)
Al2010
 
 
Posts: 50 since Oct 2010
Thanks: 36 given, 10 received


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

Reply With Quote


futures io Trading Community Platforms and Indicators NinjaTrader > Substitution for booleans in C#?


Last Updated on November 29, 2010


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Our 12-year anniversary w/ $$,$$$ prizes (check soon)

August
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
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.
no new posts