Among the many diagnostic classes
the .NET Framework provides are the Debug classes. The Debug class helps us
debug code, the Debug class is intended for debug builds.
Table 21.3 describe the members of
the Debug classes.
Table 21.3: Debug Class Members

You must define the DEBUG compiler directives to activate debugging
respectively. If you neglect to define either of these directives, Debug calls will be ignored during compilation. You can define these in
Visual Studio .NET by choosing Properties from the Project menu.
you debugged things in your
testing phase, you may find it difficult to find some of the fuzzy problems,
particularly in distributed n-tier environments. For example, some types of
problems arise only when the application is under an intense load, or the
problems may occur randomly or intermittently. The output methods of the Debug
classes are Write(), WriteIf(), WriteLine(), and
WriteLineIf(). WriteLine puts a carriage return and line feed (CRLF) on the end
of the output, but Write does not.
The Indent() and UnIndent() methods of the Debug classes increase and decrease the
current IndentLevel, which determines how much the output will be indented by
one, respectively. The Fail() method of the Debug classes produces an error
message so you can handle exceptions and errors during debugging more easily.
The Flush() method of the Debug classes forces buffered data to pour to the
Listeners collection immediately. We will talk about listeners later. The
Close() method of the Debug classes performs a Flush() implicitly and then
closes the Listeners.
The VB.NET compiler provides the following techniques to define compilation
variables:
-
Compiler command-line switches. For example, /define:DEBUG or /d:DEBUG.
-
Environment variables in the operating system shell. For example, SET DEBUG=1.
-
Pragmas in the source code. For example, #define DEBUG to define the compilation variable or #undef DEBUG to undefine it.
Conditional Debug Attributes
You can use a conditional attribute in front of the method declaration so your
method is callable when the condition is true. Conditional attributes, like
other VB.NET attributes, do not put any burden on the code calling them. For
example, in Listing 21.8, if you put #Const
IwantDEBUG = True at the top of your code
or compile your code with the /D:IwantDEBUG option, then myMethod can be called.
Otherwise, the Microsoft intermediate language code for the myMethod code will
be omitted (not generated) in your final assembly.
Listing 21.8: Conditional Attribute Example 1
Imports
System.Diagnostics
Class
Test
<Conditional("IwantDEBUG")> _
Shared Sub
myMethod()
Console.WriteLine("only for conditional
DEBUG...")
End Sub
Shared Sub
Main()
myMethod()
Console.ReadLine()
End Sub
End Class
Listing 21.9 is a more sophisticated example of using conditional attributes.
Listing 21.9: Conditional Attribute Example 2 (debug2.vb)
#Const saygin =
True //Optional
#Const
alex = True //Optional
Imports
System.Diagnostics
Public Class Debugging
Private m_r As Integer = 0
Public Sub New(ByVal r
As Integer)
Me.m_r = r
End Sub
Public
Property R() As Integer
Get
Return m_r
End Get
Set(ByVal
value As Integer)
m_r = value
End Set
End
Property
Public Shared Sub Main()
methodA()
methodB()
methodC()
methodD()
methodE()
methodF()
End Sub
Public Shared Sub methodA()
Console.WriteLine("Method A")
End Sub
<Conditional("saygin")> _
Public Shared Sub methodB()
Console.WriteLine("Method B - has [conditional(saygin)]")
End Sub
Public Shared Sub methodC()
Console.WriteLine("Method C")
End Sub
Public Shared Sub methodD()
Console.WriteLine("Method D")
End Sub
<Conditional("alex")>
_
Public Shared Sub methodE()
Console.WriteLine("Method E - has [conditional(alex)]")
End Sub
<Conditional("saygin"),
Conditional("alex")> _
Public Shared Sub methodF()
Console.WriteLine("Method F - has [conditional(saygin),conditional(alex)]")
End Sub
End Class
Output Windows

Conclusion
Hope this
article would have helped you in understanding the Debugging in VB.NET.