Home All Groups Group Topic Archive Search About
Author
21 Mar 2006 4:10 PM
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.

Author
21 Mar 2006 5:00 PM
Andrew Morton
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
Author
21 Mar 2006 7:29 PM
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!

Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:OVO7kkQTGHA.736@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
>
Author
21 Mar 2006 5:28 PM
Herfried K. Wagner [MVP]
"Arthur Dent" <hitchhikersguideto-n***@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/>
Author
21 Mar 2006 5:57 PM
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

Show quoteHide quote
"Arthur Dent" <hitchhikersguideto-n***@yahoo.com> schreef in bericht
news:OBlyBIQTGHA.5496@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.
>
Author
21 Mar 2006 6:36 PM
Tim Ferguson
"Arthur Dent" <hitchhikersguideto-n***@yahoo.com> wrote in
news:OBlyBIQTGHA.5496@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
Author
22 Mar 2006 1:16 PM
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


Show quoteHide quote
"Tim Ferguson" <Ferguso***@softhome.net> wrote in message
news:Xns978DBD59171C3garbleme4455656@207.46.248.16...
| "Arthur Dent" <hitchhikersguideto-n***@yahoo.com> wrote in
| news:OBlyBIQTGHA.5496@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
|
Author
22 Mar 2006 11:42 PM
Tim Ferguson
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
news:#CCCNLbTGHA.5264@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
Author
21 Mar 2006 6:43 PM
David Anton
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



Show quoteHide quote
"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.
>
>
>
Author
21 Mar 2006 7:29 PM
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. :)


Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> wrote in message
news:97078DFA-E003-4275-BD7E-017E5141C83B@microsoft.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.
>>
>>
>>
Author
21 Mar 2006 7:45 PM
Herfried K. Wagner [MVP]
"Arthur Dent" <hitchhikersguideto-n***@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/>
Author
21 Mar 2006 11:42 PM
Mythran
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:Og7N%23$RTGHA.4300@TK2MSFTNGP14.phx.gbl...
> "Arthur Dent" <hitchhikersguideto-n***@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
Author
22 Mar 2006 5:46 AM
Cor Ligthert [MVP]
Mythran,

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

http://www.vb-tips.com/default.aspx?ID=34241f3e-16ff-4e87-86d1-2b03e1a439ae

I hope this helps,

Cor
Author
22 Mar 2006 9:10 AM
guy
lol:)

Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> Mythran,
>
> Maybe can you than use the Eval function and use JScript.
>
> http://www.vb-tips.com/default.aspx?ID=34241f3e-16ff-4e87-86d1-2b03e1a439ae
>
> I hope this helps,
>
> Cor
>
>
>
Author
22 Mar 2006 1:22 PM
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


Show quoteHide quote
"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:%23msjoEUTGHA.5808@TK2MSFTNGP12.phx.gbl...
|
| "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
| news:Og7N%23$RTGHA.4300@TK2MSFTNGP14.phx.gbl...
| > "Arthur Dent" <hitchhikersguideto-n***@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
|
Author
22 Mar 2006 9:15 AM
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 :))

Show quoteHide quote
"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.
>
>
>