Question Asker Question Asker - 3 months ago 8
Vb.net Question

Make any calls to a function only compile in debug

The

Debug
class is useful as it allows debugging code to be written without it needing to be removed on release since any calls involving the class isn't compiled then.

How do I make my own custom Module or Class which does the same? For example, I've written this:

Module MyDebug

Sub print(ByVal msg As String)

Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)

End Sub
End Module


The only solution I can find is to use macros, but that seems to be ridiculous because I have to write something like:

#If DEBUG
MyDebug.Print("...")
#endif


at every point I need to debug in the code compared to the one-liner if I were to use
Debug.Print("...")
.

Answer

You want to tag your methods with the <Conditional> Attribute.

Module MyDebug

    <Conditional("DEBUG")>
    Sub Print(ByVal msg As String)
        Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)
    End Sub
End Module

MSDN ConditionalAttribute

Reference Source for Debug class that uses the attribute

Comments