IIF with Array

S

Shawn

OK, Please tell me if I am doing something wrong here...

This does NOT work if args.Length = 0 (It gives an
IndexOutOfRangeException on the args() array)
Public Shared Sub Main(ByVal args() As String)
Dim val As String = IIf(args.Length > 0, args(0).ToString,
"10")
Console.WriteLine("Waiting for workflow completion ({0}
seconds...).", val)
End Sub

This DOES work if args.Length = 0
Public Shared Sub Main(ByVal args() As String)
Dim val As String
If args.Length > 0 Then
val = args(0)
Else
val = "10"
End If
Console.WriteLine("Waiting for workflow completion ({0}
seconds...).", val)
End Sub

Am I wrong to assume that IIF can be used this way?

Thanks,
Shawn
 
A

Armin Zingler

Shawn said:
OK, Please tell me if I am doing something wrong here...

This does NOT work if args.Length = 0 (It gives an
IndexOutOfRangeException on the args() array)
Public Shared Sub Main(ByVal args() As String)
Dim val As String = IIf(args.Length > 0, args(0).ToString,
"10")
Console.WriteLine("Waiting for workflow completion ({0}
seconds...).", val)
End Sub

This DOES work if args.Length = 0
Public Shared Sub Main(ByVal args() As String)
Dim val As String
If args.Length > 0 Then
val = args(0)
Else
val = "10"
End If
Console.WriteLine("Waiting for workflow completion ({0}
seconds...).", val)
End Sub

Am I wrong to assume that IIF can be used this way?

Yes. IIf is a function. Before a function is called, all arguments are
calculated in order to be passed. So, VB evaluates
"args.length > 0", "args(0).ToString" and "10". While calculating the 2nd
argument, the error occurs.


Armin
 
S

Stephany Young

Yes!

To use IIf, both the TruePart and the FalsePart MUST be evaluatable.

In the case of args.Length = 0 then args(0).ToString cannot be evaluated
because the array, (args), does not have any elements. IIf thefore,
correctly, throws an exception.

And, yes, (before anyone jumps down my throat), I did read that VB.Net 2008
has a 'proper' ternary operator.
 
J

Jack Jackson

Yes. IIf is a function. Before a function is called, all arguments are
calculated in order to be passed. So, VB evaluates
"args.length > 0", "args(0).ToString" and "10". While calculating the 2nd
argument, the error occurs.


Armin

Because of this, and because IIF always returns an Object which then
must be cast back to the right type, I never use it (yes, you can
define a generic IIF, but that still leaves the problem of both
arguments being evaluated).

You apparently are not using STRICT ON. I strongly urge you to. It
makes you use some casts, but you can find a lot of bugs with it on.
 
C

Cor Ligthert[MVP]

You apparently are not using STRICT ON. I strongly urge you to. It
makes you use some casts, but you can find a lot of bugs with it on.

Yea, I don't understand why Armin never points people on this.

(You have to be a very long time regular from this newsgroup to understand
the meaning from what I wrote)

:)

Cor
 
C

Cor Ligthert[MVP]

And, yes, (before anyone jumps down my throat), I did read that VB.Net
2008 has a 'proper' ternary operator.
However a ternary operator stays in my idea something that is hard to
understand by maintaining and therefore in my idea more meant for scripting;
Where that is standard difficult.

Cor
 
M

Michael C

Cor Ligthert said:
However a ternary operator stays in my idea something that is hard to
understand by maintaining and therefore in my idea more meant for
scripting; Where that is standard difficult.

As long as it's kept simple I don't see the problem:

int x = (SomeCheck ? 1 : 0)

Surely not too hard to maintain?

Michael
 
S

Stephany Young

What's to understand?

If one understands the concept of If ... Then ... Else then one already
understands the concept of a ternary operator.
 
C

Cor Ligthert [MVP]

Michael,
As long as it's kept simple I don't see the problem:

int x = (SomeCheck ? 1 : 0)

Surely not too hard to maintain?
You are rigth

However, now this one and it seems that people to like it for things like
this.

TelBijBonus(Verkoper).Value = (Klant="Brabant" and Provincie <> "Gelderland"
And HeeftKabel And HeeftBetaald ? BerekenWinst :
BerekenAanmaning.ResultaatZero)

I don't know it it is syntyntical complete however you understand probably
what I mean?

:)

Cor
 
C

Cor Ligthert [MVP]

If one understands the concept of If ... Then ... Else then one already
understands the concept of a ternary operator.
Try to find out what I did mean with my piece of sample code to Michael,

When nicely done with an if then this is easy to read for me, this one I
made is something I have to put in pieces first.

However maybe it is me.


Cor
 
M

Michael C

Cor Ligthert said:
You are rigth

However, now this one and it seems that people to like it for things like
this.

TelBijBonus(Verkoper).Value = (Klant="Brabant" and Provincie <>
"Gelderland" And HeeftKabel And HeeftBetaald ? BerekenWinst :
BerekenAanmaning.ResultaatZero)

I don't know it it is syntyntical complete however you understand probably
what I mean?

I've seen this arguement raised before. Just because something can be abused
does not mean that it will be abused and should therefore be removed. Pretty
much everything can be abused, even the standard if statement.

Michael
 
J

Jay B. Harlow [MVP - Outlook]

Shawn,
As the others have pointed out, IIf is a function; all arguments will be
evaluated, prior to VB 2008 I would use a generic IIf to get around the
casting problem.

http://www.tsbradley.net/Cookbook/Generics/genericIIf.aspx

Starting with VB 2008 I now use the If operator.

Dim val As String = If(args.Length > 0, args(0).ToString, "10")

There are two versions of the If operator. The 3 argument version above that
replaces the IIf, and a 2 argument version similar to IsNull in SQL Server.

http://msdn2.microsoft.com/en-us/library/bb513985(VS.90).aspx

NOTE: You can introduce a Generic IIf that has an Obsolete attribute

<Obsolete> _
Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T
If expression Then
Return truePart
Else
Return falsePart
End If
End Function

Which will highlight every place your using the function allowing you to
change over to the operator.
 

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