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

Exclude code portions with the Conditional attribute

Total Hit ( 3243)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


VB developers have always used the #IF compiler directive to include or esclude portions of code from the application. The problem with this directive is that you can easily exclude a procedure with a single directive, but it isn't easy to discard all the calls to that procedure (which would raise a compilation error if you discard the target procedure).

VB.NET gives you a great alternative by means of the Conditional attribute. If you mark a procedure with this attribute the procedure itself will NOT be discarded, but all the calls to it will:

Click here to copy the following block
<Conditional("LOG")> Sub LogMsg(ByVal MsgText As String)
  Console.WriteLine(MsgText)
End Sub

Sub TestConditionalAttribute()
  LogMsg("Program is starting")
  ' ..
  LogMsg("Program is ending")
End Sub

You can define compilation constants - LOG in the above case - in the Build page of the Project Properties dialog box. You can also use the already-defined TRACE and DEBUG compilation constants. Because the compiler can drop all the calls to the target method- LogMsg, in precedingvious example - the Conditional attribute can only work only with procedures that don't return a value. If your programming logic requires that a value be returned to the caller, you can use ByRef arguments.



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.