Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Write concise code with the IIf function

Total Hit ( 2192)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Use IIf when setting a value based upon an expression. This works only in situations where you have two choices, based upon the result of a Boolean expression.

Click here to copy the following block
'long way
If Var1 > Var2 Then
  Var3 = Var1 / Var2
Else
  Var3 = Var2 / Var1
End If

'this is more concise
Var3 = IIf(Var1 > Var2, Var1 / Var2, Var2 / Var1)

The IIf function is often useful to evaluate the minimum or the maximum of two numbers:

Click here to copy the following block
' the minimum of n1, n2
min = IIf(n1 <= n2, n1, n2)

' the maximum of n1, n2
max = IIf(n1 >= n2, n1, n2)

Note, however, that the IIf syntax takes nearly 3 times as long to execute. Although it compacts the code, it should not be used in situations requiring many repetitions of the code. Moreover, IIf works ONLY in cases where all expressions execute correctly at run-time. In the above example, if either Var1 or Var2 is zero, the "short way" will always generate a run time error as all expressions on the line are evaluated.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.