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

Providing a default value for optional arguments

Total Hit ( 3360)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Unlike VB6, VB.NET requires that you specify the default value of any Optional argument. In general you should use a value that is invalid under normal circumstances, so that the called procedure can discern whether the argument has been actually passed or not. For example, you should use -1 as a special value if the argument shouldn't take negative values; or you can use the largest negative or positive number for that numeric type. In the latter case, you might use the MinValue and MaxValue properties that all numeric classes expose:

Click here to copy the following block
Sub MyProc(Optional ByVal X As Long = Long.MinValue)
  If X = Long.MinValue Then
    ' The X argument has been omitted (presumably).
    Console.WriteLine(X) ' => -9223372036854775808
  End If
  ' ....
End Sub

If the optional argument is a Single or Double, you can also use the special NaN (Not-a-Number) value for its default:

Click here to copy the following block
Sub MyProc(Optional ByVal X As Double = Double.NaN)
  If Double.IsNaN(x) Then
    ' The X argument has been omitted.
  End If
End Sub


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.