473,379 Members | 1,245 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,379 software developers and data experts.

what's the difference

what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e

TIA, z.
Nov 21 '05 #1
12 1741
* "z. f." <zi**@info-scopeREMSPAM.co.il> scripsit:
what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e


In VB.NET, 'd' and 'e' will be evaluated and boxed in an 'Object'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
z. f.

Last I checked, the IIF was a function in VB, not an operator, so it had
the side effect of evaluating the false condition, even when the condition
was true. So if you did something like this:

a = Iif(b = c, GetD(), GetE())

Then both GetD and GetE would be called, but only one would be returned.
This can have nasty side effects.

In C#, the ? operator will not evaluate the other condition if it is not
met.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"z. f." <zi**@info-scopeREMSPAM.co.il> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e

TIA, z.

Nov 21 '05 #3
z. f. wrote:
what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e


Just look at the IL code (appended)

In short: C# has the faster solution.

VB.NET:
..maxstack 3
.locals init ([0] int16 a,
[1] int16 b,
[2] int16 c,
[3] int16 d,
[4] int16 e)
IL_0000: nop
IL_0001: ldloc.1
IL_0002: ldloc.2
IL_0003: ceq
IL_0005: ldloc.3
IL_0006: box [mscorlib]System.Int16
IL_000b: ldloc.s e
IL_000d: box [mscorlib]System.Int16
IL_0012: call object [Microsoft.VisualBasic]
Microsoft.VisualBasic.Interaction::IIf(bool, object, object)
IL_0017: call int16 [Microsoft.VisualBasic]
Microsoft.VisualBasic.CompilerServices.ShortType:: FromObject(object)
IL_001c: stloc.0
IL_001d: nop
IL_001e: ret


C#:
..maxstack 2
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d,
[4] int32 e)
IL_0000: ldc.i4.0
IL_0001: stloc.1
IL_0002: ldc.i4.0
IL_0003: stloc.2
IL_0004: ldc.i4.0
IL_0005: stloc.3
IL_0006: ldc.i4.0
IL_0007: stloc.s e
IL_0009: ldloc.1
IL_000a: ldloc.2
IL_000b: beq.s IL_0011
IL_000d: ldloc.s e
IL_000f: br.s IL_0012
IL_0011: ldloc.3
IL_0012: stloc.0
IL_0013: ret

--
Greetings
Jochen
Nov 21 '05 #4
z. f. <zi**@info-scopeREMSPAM.co.il> wrote:
what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e


Firstly, iif evaluates every function in every expression - C# only
evalutes the "testing" expression and then whichever of the expressions
is going to be returned. Here's a pair of complete programs to show the
difference:

C#:
using System;

public class Test
{
static void Main()
{
int x = ReturnTrue() ? Return1() : Return2();
}

static bool ReturnTrue()
{
Console.WriteLine ("ReturnTrue called");
return true;
}

static int Return1()
{
Console.WriteLine ("Return1 called");
return 1;
}

static int Return2()
{
Console.WriteLine ("Return2 called");
return 2;
}
}
VB.NET:
Option Strict On

Imports System
Imports Microsoft.VisualBasic

Class Test

Shared Sub Main
Dim x as Object = IIf (ReturnTrue, Return1, Return2)
End Sub

Shared Function ReturnTrue As Boolean
Console.WriteLine ("ReturnTrue called")
ReturnTrue = true
End Function

Shared Function Return1 As Integer
Console.WriteLine ("Return1 called")
Return1 = 1
End Function

Shared Function Return2 As Integer
Console.WriteLine ("Return2 called")
Return2 = 2
End Function

End Class

Output from C# version:
ReturnTrue called
Return1 called

Output from VB.NET version:
ReturnTrue called
Return1 called
Return2 called

Secondly, the conditional operator in C# has specific requirements
about the types of the second and third expressions which allow the
expression as a whole to have an appropriate type (rather than just the
Object of VB.NET's IIF).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 21 '05 #5
z.
As Hefried suggests, iif in VB.NET is a function call.

It actually & directly calls a function in the
Microsoft.VisualBasic.Interaction module, because you are calling a
function, both arguments have to be evaluated.

Where as operator ? is an operator, it is compiled directly into IL code.
because it is IL code, it is able to short circuit and only evaluate the
argument that needs to be returned.

Hope this helps
Jay

"z. f." <zi**@info-scopeREMSPAM.co.il> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
what's the difference between using the VB.NET function IIF or the c#
operator ?
VB.NET: a = iif ( b=c, d, e)
C#: a= (b==c)?d:e

TIA, z.

Nov 21 '05 #6
ZF,

Why you asking this.
VB.NET: a = iif ( b=c, d, e) Can only be used in VBNet
C#: a= (b==c)?d:e

Can only be used in C#

What is the relevance of your question?

Cor
Nov 21 '05 #7
Patty,

VB can not use the ? syntax. As for if it is a "more poor" language
than C#, well, I'm staying out of that one.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Patty O'Dors" <Pa********@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
can VB.NET not use the ? : syntax?
If so, then use it, if not - that's another reason why it's more of a poor
language than C#.

"Jochen Kalmbach" wrote:
z. f. wrote:
> what's the difference between using the VB.NET function IIF or the c#
> operator ?
> VB.NET: a = iif ( b=c, d, e)
> C#: a= (b==c)?d:e


Just look at the IL code (appended)

In short: C# has the faster solution.

VB.NET:
..maxstack 3
.locals init ([0] int16 a,
[1] int16 b,
[2] int16 c,
[3] int16 d,
[4] int16 e)
IL_0000: nop
IL_0001: ldloc.1
IL_0002: ldloc.2
IL_0003: ceq
IL_0005: ldloc.3
IL_0006: box [mscorlib]System.Int16
IL_000b: ldloc.s e
IL_000d: box [mscorlib]System.Int16
IL_0012: call object [Microsoft.VisualBasic]
Microsoft.VisualBasic.Interaction::IIf(bool, object, object)
IL_0017: call int16 [Microsoft.VisualBasic]
Microsoft.VisualBasic.CompilerServices.ShortType:: FromObject(object)
IL_001c: stloc.0
IL_001d: nop
IL_001e: ret


C#:
..maxstack 2
.locals init ([0] int32 a,
[1] int32 b,
[2] int32 c,
[3] int32 d,
[4] int32 e)
IL_0000: ldc.i4.0
IL_0001: stloc.1
IL_0002: ldc.i4.0
IL_0003: stloc.2
IL_0004: ldc.i4.0
IL_0005: stloc.3
IL_0006: ldc.i4.0
IL_0007: stloc.s e
IL_0009: ldloc.1
IL_000a: ldloc.2
IL_000b: beq.s IL_0011
IL_000d: ldloc.s e
IL_000f: br.s IL_0012
IL_0011: ldloc.3
IL_0012: stloc.0
IL_0013: ret

--
Greetings
Jochen

Nov 21 '05 #8
Cor Ligthert wrote:
VB.NET: a = iif ( b=c, d, e)

Can only be used in VBNet


This you can also use in ANY .NET language... just reference the VB
assembly and call "Microsoft.VisualBasic.Interaction::IIf"

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
Nov 21 '05 #9
Nicholas,

In my opinion are they both as legacy as an operator can be.
When that would give an opinion of a language, it is absolute time for a
complete new one.

Cor
Nov 21 '05 #10
ZF

Jochen did give an difference
VB.NET: a = iif ( b=c, d, e) Can be used in every Net language
C#: a= (b==c)?d:e

Can only be used in languages that have legacy C operators.

Cor
Nov 21 '05 #11
* Jochen Kalmbach <no********************@holzma.de> scripsit:
VB.NET: a = iif ( b=c, d, e)

Can only be used in VBNet


This you can also use in ANY .NET language... just reference the VB
assembly and call "Microsoft.VisualBasic.Interaction::IIf"


ACK. And 'IIf' can be easily reimplemented in every .NET programming
language.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #12
Nak
Hi,
can VB.NET not use the ? : syntax?
If so, then use it, if not - that's another reason why it's more of a poor
language than C#.


LOL, apparently the new version of VB to come will let you define your own
operators, or so I read somewhere. But that does not make if a poor
language for now just because you will have to write a few more lines of
code, I mean it even indents it for you and writes the "End If" for you so
it isn't *that* hard to do!

Nick.
Nov 21 '05 #13

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
1
by: mike | last post by:
best regards: What is the difference between DOM Level 1 and DOM Level 2. Thank you. May god be with you.
5
by: radnus2004 | last post by:
Hi all, I am not clear what is the difference between signed and unsigned in C. Many say, unsigned means only +ve values and 0. Can I use unsigned int i = -3? What happens internally ? What...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
3
by: Anoj Kumar | last post by:
Hi All , can anyone tell me what is the difference between the following declaration and how it affects application performance. 1. Dim cn As ADODB.Connection Set cn = New ADODB.Connection
6
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What is the difference between a Field and a Property? Here is the context. In the VS.NET 2002 documentation, in the "String Members" section, we have the...
5
by: kai | last post by:
Hi, In ASP.NET , what is the difference between OnClick and Click events for a button? Because we have button click event, it can trigger events, why we still need OnClick? Please help. ...
49
by: Zach | last post by:
After having taken a looong break from VB (last used 6.0), I started getting back into programming again, this time with VS 2005. I began reading up on VB.NET from various sources (books,...
1
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.