Ternary Operator?

A

Arthur Dent

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.
 
A

Andrew Morton

Arthur said:
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
 
H

Herfried K. Wagner [MVP]

Arthur Dent said:
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'.
 
C

Cor Ligthert [MVP]

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
 
T

Tim Ferguson

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
 
G

Guest

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
 
A

Arthur Dent

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!
 
A

Arthur Dent

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. :)
 
H

Herfried K. Wagner [MVP]

Arthur Dent said:
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

Mythran

Herfried K. Wagner said:
'If...Then...Else...' will some in many cases, but not in all. That's
true.

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
 
G

Guest

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 :))
 
J

Jay B. Harlow [MVP - Outlook]

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/Generics/genericIIf.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


| |
| > 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
|
 
J

Jay B. Harlow [MVP - Outlook]

| 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


|
| | >> 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
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top