ARTICLE

Debugging VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language September 29, 2010
In this article I will explain you about the Debugging VB.NET.
 
Reader Level:

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

table21.3.gif

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

debug-1.gif        debug.gif

Conclusion

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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor