Download Horizon :: Staff Members :: TheTechGame.com :: XboxMB YouTube


Old 08-21-2010   #1 (permalink)
Cheater912's Avatar
:D
Join Date: Jul 2010
Location: New York
Posts: 5,683
Thanks: 12,223
Lightbulb The Ternary Operator

I've been obsessed with the ternary operator lately, so I wanted to show you guys what it is since beginners don't usually take use of it. The ternary operator (?) allows you to condense an if statement. So lets take this chunk of code for example:

PHP Code:
if (TextBox1.Text == String.Empty)
    
Button1.Enabled false;
else
    
Button1.Enabled true

That would disable Button1 if the text box (TextBox1) was empty. Using the ternary operator, you can condense that into 1 single line of code. Here's an example that has the same function as the code above:

PHP Code:
Button1.Enabled = (TextBox1.Text == String.Empty) ? false true

The code after the ternary operator (?) sets the Enabled property to false if the statement in the parentheses is true (TextBox1.Text == String.Empty).

":" is a substitute for "else". If the code in the parentheses is false, then it will use the code at the end.

You can use the ternary operator with any data type and even wrap functions around it.

PHP Code:
string MyString Base64Encode((TextBox1.Text == String.Empty) ? "No Text Entered" TextBox1.Text); 

If needed, it is also possible to embed ternary operators within ternary operators.

PHP Code:
string MyString = (statement) ? ((statement) ? "Yo" "Hey") : "Hello"


Other Examples:

PHP Code:
Button2.Enabled Button1.Enabled = (TextBox1.Text == String.Empty) ? false true
__________________
Cheater912 is offline Send a message via Skype™ to Cheater912
Reply With Quote


Old 08-21-2010   #2 (permalink)
ohara's Avatar
Join Date: Aug 2010
Location: Glasgow, Scotland.
Posts: 56
Thanks: 6
Default Re: The Ternary Operator

I tried understanding that but was unsuccessful, what are these codes used for?
ohara is offline
Reply With Quote


Old 08-21-2010   #3 (permalink)
Cheater912's Avatar
:D
Join Date: Jul 2010
Location: New York
Posts: 5,683
Thanks: 12,223
Default Re: The Ternary Operator

Quote:
Originally Posted by ohara View Post
I tried understanding that but was unsuccessful, what are these codes used for?
The code boxes are just examples. The operator just lets you condense statements into a single line.
__________________
Cheater912 is offline Send a message via Skype™ to Cheater912
Reply With Quote
The following user thanked this post: 8Bit


Old 08-21-2010   #4 (permalink)
ohara's Avatar
Join Date: Aug 2010
Location: Glasgow, Scotland.
Posts: 56
Thanks: 6
Default Re: The Ternary Operator

Quote:
Originally Posted by Cheater912 View Post
The code boxes are just examples. The operator just lets you condense statements into a single line.
No I mean like in general, I have no clue about this stuff but I would like to learn.
ohara is offline
Reply With Quote


Old 08-21-2010   #5 (permalink)
Cheater912's Avatar
:D
Join Date: Jul 2010
Location: New York
Posts: 5,683
Thanks: 12,223
Default Re: The Ternary Operator

Quote:
Originally Posted by ohara View Post
No I mean like in general, I have no clue about this stuff but I would like to learn.
Start with downloading Visual C#. It's free
__________________
Cheater912 is offline Send a message via Skype™ to Cheater912
Reply With Quote
The following users thanked this post: 8Bit, KeyboardCat




Old 08-21-2010   #6 (permalink)
ohara's Avatar
Join Date: Aug 2010
Location: Glasgow, Scotland.
Posts: 56
Thanks: 6
Default Re: The Ternary Operator

Quote:
Originally Posted by Cheater912 View Post
Start with downloading Visual C#. It's free
OK, I guess I'll give it a go.
ohara is offline
Reply With Quote


Old 08-23-2010   #7 (permalink)

Tucker's Avatar
Who Dares, Wins
Join Date: Jul 2010
Location: United Kingdom
Posts: 2,220
Thanks: 1,875
Default Re: The Ternary Operator

Two strings can enable a button to.
e.g. Could be used for a login to make sure Username and Password Text Boxes aren't null.
PHP Code:
button1.Enabled String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text) ? false true

For those who are new to programming, you would add this to both of the Text Box Text Changed events.
__________________
Quote:
Originally Posted by ElderScrolls View Post
Sorry, a lot of people on here find women as distant creatures part of a different realm.
Tucker is offline
Reply With Quote


Old 08-23-2010   #8 (permalink)
austin12456's Avatar
Join Date: Jul 2010
Posts: 1,263
Thanks: 1,166
Default Re: The Ternary Operator

^^
(x || y) is already a boolean.

Code:
this.button1.Enabled = !(String.IsNullOrEmpty(this.textBox1.Text) || String.IsNullOrEmpty(this.textBox2.Text));
__________________
austin12456 is offline
Reply With Quote
The following user thanked this post: Tucker


Old 08-26-2010   #9 (permalink)
Banned
 
Join Date: Aug 2010
Location: Massachussetts
Posts: 389
Thanks: 81
Default Re: The Ternary Operator

I tried testing if this would speed up code execution. It does execute like .1 seconds faster, but I don't see a huge reason to use this.
iCypher is offline Send a message via AIM to iCypher
Reply With Quote


Old 08-26-2010   #10 (permalink)
Cheater912's Avatar
:D
Join Date: Jul 2010
Location: New York
Posts: 5,683
Thanks: 12,223
Default Re: The Ternary Operator

Quote:
Originally Posted by iCypher View Post
I tried testing if this would speed up code execution. It does execute like .1 seconds faster, but I don't see a huge reason to use this.
I use it cause it saves time and it makes things easier.
__________________
Cheater912 is offline Send a message via Skype™ to Cheater912
Reply With Quote
The following users thanked this post: 8Bit, The Programmer

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 09:16 AM.


 

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
COPYRIGHT (c) 2010 - 2012 - XboxMB - DESIGN BY:
EDENWEBS.COM