473,411 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Ternary Operator?

Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and it
would be nice to have a concise way of writing this instead of using a whole
If-Then-Else block.

Cheers all,
- Arthur Dent.
Mar 21 '06 #1
15 12692
Arthur Dent wrote:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it?
Stuck away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use
expressions which may fail if they are evaluated when they arent
supposed to be, and it would be nice to have a concise way of writing
this instead of using a whole If-Then-Else block.


Perhaps you're looking for AndAlso and OrElse which do the evaluation
short-circuiting it sounds like you want.

If False AndAlso thisNeverGetsCalled() then...

If True OrElse thisNeverGetsCalled() then...

Andrew
Mar 21 '06 #2
"Arthur Dent" <hi*********************@yahoo.com> schrieb:
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.


VB's equivalent is

\\\
If x Then
o = a
Else
o = b
End If
///

which can be written as 'If x Then o = a Else o = b'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 21 '06 #3
Arthur,

I would not tell this as like C, it is more syntax from older
spreadsheetprograms where everything had to be done in one cell..

Cor

"Arthur Dent" <hi*********************@yahoo.com> schreef in bericht
news:OB**************@TK2MSFTNGP11.phx.gbl...
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and
it would be nice to have a concise way of writing this instead of using a
whole If-Then-Else block.

Cheers all,
- Arthur Dent.

Mar 21 '06 #4
"Arthur Dent" <hi*********************@yahoo.com> wrote in
news:OB**************@TK2MSFTNGP11.phx.gbl:
By ternary, i mean something like C's a?b:c syntax.


I think that IIf is still available:

Return CStr(IIf(testMe > 1000, "Large", "Small"))

It's in the help files. Is it deprecated for some reason?

Tim F

Mar 21 '06 #5
Right - there is no ternary operator.
IIf is clumsy for ther reasons you mention.
The closest functional equivalent is If/Else blocks (but rather unwieldy for
the kind of things you'd use the ternary operator for).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Arthur Dent" wrote:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and it
would be nice to have a concise way of writing this instead of using a whole
If-Then-Else block.

Cheers all,
- Arthur Dent.

Mar 21 '06 #6
Nice to hear someone who knows why i would want it. Most people usually say
"just use an if-then-else" which works, but is clumsy.
At least i am not the only one who gets it. :)
"David Anton" <Da********@discussions.microsoft.com> wrote in message
news:97**********************************@microsof t.com...
Right - there is no ternary operator.
IIf is clumsy for ther reasons you mention.
The closest functional equivalent is If/Else blocks (but rather unwieldy
for
the kind of things you'd use the ternary operator for).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Arthur Dent" wrote:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it?
Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use
expressions
which may fail if they are evaluated when they arent supposed to be, and
it
would be nice to have a concise way of writing this instead of using a
whole
If-Then-Else block.

Cheers all,
- Arthur Dent.

Mar 21 '06 #7
Thanks for the tip.... i love the new short circuited booleans, but hadnt
thought of using them as ternary operatory replacements.
Still not quite as elegent, but it will work better than a whole
if-then-else block.

Thanks!

"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:OV*************@TK2MSFTNGP12.phx.gbl...
Arthur Dent wrote:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it?
Stuck away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use
expressions which may fail if they are evaluated when they arent
supposed to be, and it would be nice to have a concise way of writing
this instead of using a whole If-Then-Else block.


Perhaps you're looking for AndAlso and OrElse which do the evaluation
short-circuiting it sounds like you want.

If False AndAlso thisNeverGetsCalled() then...

If True OrElse thisNeverGetsCalled() then...

Andrew

Mar 21 '06 #8
"Arthur Dent" <hi*********************@yahoo.com> schrieb:
Nice to hear someone who knows why i would want it. Most people usually
say "just use an if-then-else" which works, but is clumsy.


'If...Then...Else...' will some in many cases, but not in all. That's true.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 21 '06 #9

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Og****************@TK2MSFTNGP14.phx.gbl...
"Arthur Dent" <hi*********************@yahoo.com> schrieb:
Nice to hear someone who knows why i would want it. Most people usually
say "just use an if-then-else" which works, but is clumsy.


'If...Then...Else...' will some in many cases, but not in all. That's
true.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Just use C#! :P And no, there is no terd...err...ternary operator ...
unfortunately...IIf is as close as you can get but is dangerous for obvious
reasons. I wish there were ternary in VB.Net (as well as all other
languages too!)...

Mythran

Mar 21 '06 #10
Mythran,

Maybe can you than use the Eval function and use JScript.

http://www.vb-tips.com/default.aspx?...1-2b03e1a439ae

I hope this helps,

Cor
Mar 22 '06 #11
guy
lol:)

"Cor Ligthert [MVP]" wrote:
Mythran,

Maybe can you than use the Eval function and use JScript.

http://www.vb-tips.com/default.aspx?...1-2b03e1a439ae

I hope this helps,

Cor

Mar 22 '06 #12
guy
i used to agree with you, but i must admit it ainitially looks clumsy but
If...
stuff
Else
....morestuff
End If

reads clearer than a ternary construct a year later

regards Ford

btw

42 = 6 x 9 in base 13 :))

"Arthur Dent" wrote:
Hi all, im just curious if anyone knows.....
With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck
away in a corner somewhere?
By ternary, i mean something like C's a?b:c syntax.
IIf works in most cases, but in some instances you want to use expressions
which may fail if they are evaluated when they arent supposed to be, and it
would be nice to have a concise way of writing this instead of using a whole
If-Then-Else block.

Cheers all,
- Arthur Dent.

Mar 22 '06 #13
Tim,
| It's in the help files. Is it deprecated for some reason?
IIf is not deprecated, however you need to be aware of how to use it!

Its a function!!! Which means that both operands are evaluated.

For example:

Dim index As Integer = 1000

Dim list(100) as String
Dim value As String = IIf(index < list.Length, list(index), "out of
range")

Will cause an index out of range exception as list(index) will be evaluated
& passed to the IIf function, the function then returns either the true part
of the false part...
IIf has a couple of other "problems", its not Option Strict On friendly and
it's parameters & return value are Object. In other words with Option Strict
On, I would need to cast the return value above back to String. The
parameters being Object, means that value types (structures) will be boxed &
unboxed when you call IIf, which itself may be a slight performance problem
and will add pressure to the GC...
If your using .NET 2.0 I would recommend a Generic IIf instead:

http://www.tsbradley.net/Cookbook/Ge...enericIIf.aspx

My generic IIf avoids the Option Strict On & Object boxing problems, however
its still a function.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Tim Ferguson" <Fe********@softhome.net> wrote in message
news:Xn*****************************@207.46.248.16 ...
| "Arthur Dent" <hi*********************@yahoo.com> wrote in
| news:OB**************@TK2MSFTNGP11.phx.gbl:
|
| > By ternary, i mean something like C's a?b:c syntax.
|
| I think that IIf is still available:
|
| Return CStr(IIf(testMe > 1000, "Large", "Small"))
|
| It's in the help files. Is it deprecated for some reason?
|
| Tim F
|
Mar 22 '06 #14
| I wish there were ternary in VB.Net (as well as all other
| languages too!)...
I would like to see a true ternary operator in VB.NET, however I don't want
something as "obscure" as C's "a?b:c"

I like the IIf syntax, however its a function, changing it to an actual
inline operator may break code. (of course in many cases that breakage may
actually be helping the code ;-))

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
| news:Og****************@TK2MSFTNGP14.phx.gbl...
| > "Arthur Dent" <hi*********************@yahoo.com> schrieb:
| >> Nice to hear someone who knows why i would want it. Most people usually
| >> say "just use an if-then-else" which works, but is clumsy.
| >
| > 'If...Then...Else...' will some in many cases, but not in all. That's
| > true.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
| Just use C#! :P And no, there is no terd...err...ternary operator ...
| unfortunately...IIf is as close as you can get but is dangerous for
obvious
| reasons. I wish there were ternary in VB.Net (as well as all other
| languages too!)...
|
| Mythran
|
Mar 22 '06 #15
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
news:#C**************@TK2MSFTNGP10.phx.gbl:

Its a function!!! Which means that both operands are evaluated.


Whoops: I knew that... teach me to read the question a bit more closely!

B Wishes

Tim F

Mar 22 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
4
by: Bob Gregory | last post by:
Hi all, I don't actually have a mac with which to test this, but I'm informed by a colleague that one of my scripts has failed in IE on the Mac; endless twiddling seems to point to the ternary...
24
by: gupta.keshav | last post by:
HI, Is there any situation which can be handled by ternary operator but not with if else blocks? Thanks Keshav
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.