473,320 Members | 2,145 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,320 software developers and data experts.

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

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
Oct 11 '06 #1
11 7299
Philipp Brune wrote:
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,

--

(O)enone
Oct 11 '06 #2
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
Oct 11 '06 #3
Robinson wrote:
>I think the closest you'll get to that is by using IIf:
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.

--

(O)enone
Oct 11 '06 #4
If this is for display you could also use databinding and the Format/Parse
handlers...

--
Patrice

"Philipp Brune" <ph***********@t-online.dea écrit dans le message de news:
eg*************@news.t-online.com...
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

Oct 11 '06 #5
"Philipp Brune" <ph***********@t-online.deschrieb:
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)').

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

Oct 11 '06 #6
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.

Oct 11 '06 #7

<te******@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>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
Oct 11 '06 #8
Mythran wrote:
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")

Oct 12 '06 #9
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.
<te******@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Mythran wrote:
>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")

Oct 12 '06 #10
Oenone & others,
Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1).ToString
If I need an IIf I normally use my Generic IIf, avoiding the need of
ToString or other awkward casts:

Dim s2 As String = IIf(s1 Is Nothing, "(undefined)", s1)

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

I have also simply overloaded IIf in .NET 1.x where the benefits of Generic
functions are not available...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Oenone" <oe****@nowhere.comwrote in message
news:9i**************@newsfe3-gui.ntli.net...
Philipp Brune wrote:
>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,

--

(O)enone
Oct 12 '06 #11
Jay B. Harlow wrote:
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.

--

(O)enone
Oct 13 '06 #12

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

Similar topics

3
by: Robert Dodier | last post by:
Hello, Here's a thought that I'm sure has already occurred to someone else, I just can't find any record of it yet. An XML document is just a more verbose and clumsy representation of an...
27
by: junky_fellow | last post by:
Is *ptr++ equivalent to *(ptr++) ?
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
9
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
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...
14
by: grid | last post by:
Hi, I have a certain situation where a particular piece of code works on a particular compiler but fails on another proprietary compiler.It seems to have been fixed but I just want to confirm if...
1
by: mkalyan79 | last post by:
Hi, I wanted to find out what is the equivalent operator of ">>>" operator of java in C#. Thanks, K
6
by: mearvk | last post by:
Does C++ or C have something roughly equivalent to this: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html What I need is a way to quickly convert between decimal and binary and...
6
by: Bob Altman | last post by:
Hi all, What's the C++ equivalent for the VB TypeOf operator. For example: void ReportError(System::Exception^ ex) { // Psuedo-VB syntax if (TypeOf ex Is System::OperationCanceledException)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.