Substitution for booleans in C#? - NinjaTrader Programming | futures io social day trading
futures io futures trading


Substitution for booleans in C#?
Updated: Views / Replies:1,001 / 9
Created: by Al2010 Attachments:0

Welcome to futures io.

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

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

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

Reply
 
Thread Tools Search this Thread
 

Substitution for booleans in C#?

  #1 (permalink)
Trading for Fun
 
Futures Experience: None
Platform: NinjaTrader
 
Posts: 50 since Oct 2010
Thanks: 36 given, 10 received

Substitution for booleans in C#?

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
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Paris
 
Futures Experience: None
Platform: NinjaTrader
 
Posts: 1,177 since Oct 2009
Thanks: 228 given, 4,097 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:
 
  #4 (permalink)
 Vendor: www.traderwerks.com 
Taipei Taiwan
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Optimus
Favorite Futures: TW
 
Posts: 693 since Jun 2009
Thanks: 440 given, 440 received

Try a switch statement.

Reply With Quote
The following user says Thank You to traderwerks for this post:
 
  #5 (permalink)
Trading for Fun
 
Futures Experience: None
Platform: NinjaTrader
 
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
 
  #6 (permalink)
Trading for Fun
 
Futures Experience: None
Platform: NinjaTrader
 
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
 
  #7 (permalink)
Elite Member
North Bend, WA
 
Futures Experience: Beginner
Platform: Tradestation, C#
Broker/Data: Tradestation
Favorite Futures: None yet
 
BDawg's Avatar
 
Posts: 15 since Oct 2010
Thanks: 63 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-


Last edited by BDawg; November 24th, 2010 at 11:00 AM.
Reply With Quote
The following 2 users say Thank You to BDawg for this post:
 
  #8 (permalink)
Elite Member
Paris
 
Futures Experience: None
Platform: NinjaTrader
 
Posts: 1,177 since Oct 2009
Thanks: 228 given, 4,097 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:
 
  #9 (permalink)
Elite Member
North Bend, WA
 
Futures Experience: Beginner
Platform: Tradestation, C#
Broker/Data: Tradestation
Favorite Futures: None yet
 
BDawg's Avatar
 
Posts: 15 since Oct 2010
Thanks: 63 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:
 
  #10 (permalink)
Trading for Fun
 
Futures Experience: None
Platform: NinjaTrader
 
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

Reply



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

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     


All times are GMT -4. The time now is 07:02 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, 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
Page generated 2017-12-16 in 0.12 seconds with 18 queries on phoenix via your IP 54.145.16.43