Exclude code portions with the Conditional attribute

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:

 Sub LogMsg(ByVal MsgText As String)    Console.WriteLine(MsgText)End SubSub 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS