VB.NET equivalent of C# Operator '??'

P

Philipp Brune

Hello Newsgroup,

in c# there exists the '??' operator.

It is used like this :

string s1 = null;
string s2 = s1 ?? "(undefined)"

And does the following (simplified) :

if(s1 == null)
s2 = "(undefined)"
else
s2 = s1

Is there an equivalent operator in Visual Basic.Net, and if so, how is
it called ?

Thanks in advance

Philipp
 
O

Oenone

Philipp said:
Is there an equivalent operator in Visual Basic.Net, and if so, how is
it called ?

I think the closest you'll get to that is by using IIf:

\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1).ToString
///

HTH,
 
R

Robinson

I think the closest you'll get to that is by using IIf:

\\\
Dim s1 As String = Nothing
Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1).ToString
///

Yes, however, what is more readable? This:

If s1 Is Nothing Then
s2 = "(undefined)"
Else
s2 = s1
End If



or this:



s2 = IIf(s1 Is Nothing, "(undefined)", s1).ToString



We also had this debate at work and decided the former was better (in the
context of C++, not .NET, ie. z = ( v < w ? x : y ) ). These functions do
well to help obfuscate code however ;)



Robin
 
O

Oenone

Yes, however, what is more readable?

I'd certainly say the "full" version is more readable and maintainable than
the abbreviated IIf version.

Personally the only thing I normally use IIf for is adding plurals to status
message, for example:

\\\
MsgBox("Downloaded " & msgcount &" message" & IIf(msgcount=1, "",
"s").ToString)
///

In other situations (and arguably in this one too) they nearly always make
the code harder to read.
 
P

Patrice

If this is for display you could also use databinding and the Format/Parse
handlers...
 
H

Herfried K. Wagner [MVP]

Philipp Brune said:
in c# there exists the '??' operator.

It is used like this :

string s1 = null;
string s2 = s1 ?? "(undefined)"

And does the following (simplified) :

if(s1 == null)
s2 = "(undefined)"
else
s2 = s1

Is there an equivalent operator in Visual Basic.Net, and if so, how is it
called ?

No, there is no such operator in VB. Note that '??' performs additional
operations for nullable types ('Nullable(Of T)').
 
T

teslar91

I like the brevity of IIF in certain cases, and do use it. Sometimes
expanding code horizontally reduces readability less than expanding
vertically.

Just remember that unlike If/Then/Else/EndIf, it evaluates *both* true
and false expressions all the time, and so can cause unexpected errors.
For example, if the false expression is invalid when the condition is
true, you still get an error. It's also slower for that reason, and
recently I've found reason to suspect it does some unnecessary type
conversions behind-the-scenes as well.
 
M

Mythran

I like the brevity of IIF in certain cases, and do use it. Sometimes
expanding code horizontally reduces readability less than expanding
vertically.

Just remember that unlike If/Then/Else/EndIf, it evaluates *both* true
and false expressions all the time, and so can cause unexpected errors.
For example, if the false expression is invalid when the condition is
true, you still get an error. It's also slower for that reason, and
recently I've found reason to suspect it does some unnecessary type
conversions behind-the-scenes as well.

The IIf function doesn't do anything special...

How would you write a function named IIf?

I bet something like:

Public Function IIF(ByVal Expression As Boolean, ByVal ReturnTrue As Object,
ByVal ReturnFalse As Object) As Object
If Expression
Return ReturnTrue
Else
Return ReturnFalse
End If
End Function

eh? Nothing special here. Just return ReturnTrue if the expression passed
into the function is true, otherwise return ReturnFalse. No unnecessary
type conversions, no slowness due to it doing anything special. I bet the
slowness is caused by only the unnecessary function call to IIf rather than
performing the if logic yourself.

Hmm...HTH,
Mythran
 
T

teslar91

Mythran said:
The IIf function doesn't do anything special...

How would you write a function named IIf?

I bet something like:

Public Function IIF(ByVal Expression As Boolean, ByVal ReturnTrue As Object,
ByVal ReturnFalse As Object) As Object
If Expression
Return ReturnTrue
Else
Return ReturnFalse
End If
End Function
<clip>

That's exactly the way I imagine it. And there lies the source of the
issues I mentioned.

Consider:

If Flag Then
AString = BString & CString
Else
AString = CString & BString
End If

Versus:

AString=Iif(Flag, BString & CString, CString & BString)

In the If/Then/Else/EndIf version, *one* string concatenation is
performed, regardless of whether Flag is True or False. But in the Iif
version, *two* string concatenations are performed, as both the
ReturnTrue and ReturnFalse parameters must be fully evaluated before
being passed to the Iif function for selection.

Normally this doesn't make much difference; but it can if you've got
some slow-evaluating expressions, or you're trying to squeeze every
possible bit of speed out of an inner loop.

And that's also the reason why this code will handle Divisor = 0:

If Divisor <> 0 Then
TextBox1.Text = Dividend / Divisor
Else
TextBox1.Text = "Overflow"
End If

And this seemingly equivalent code will fail:

TextBox1.Text = Iif(Divisor <> 0, Dividend / Divisor, "Overflow")
 
S

Stephany Young

It's even simpler than that.

It is actually implemented as:

If Expression Then Return ReturnTrue
Return ReturnFalse

which avoids the overhead of the Else, however small that may be.

The point is, as you have spotted, that the bottleneck on IIf, is in the
evaluation of the expression and the true and false parts in the parameter
assignment part of the call, rather than inside the method itself.

The other point about IIf, is that the true and false parameters are both of
type Object, as is the return value. This means that the return value, in
most cases, must be converted to the required type to make the result
useful.
 
O

Oenone

Jay said:
If I need an IIf I normally use my Generic IIf, avoiding the need of
ToString or other awkward casts

Ah, very handy, thanks for that.

I'd tended to use my own string-based implementation of IIf, as it's nearly
always strings that I use as its return values, but the Generic version is
much nicer.
 

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