(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue
April 2002
Section
Black Belt column

Listing 1
Leave a trail of bread crumbs in your code
Take the trouble to instrument your code as you build it and you'll reduce debugging problems by at least an order of magnitude. This ImageDirectoryMgr Class Constructor     from our test bed application shows the constructor for one of the .NET classes we've instrumented. Note the use of both Debug and Trace class instrumentation in this constructor. 

Public Sub New(ByVal directoryPath As String)
	'purpose: create a new directory object inside the object's contructor
	'write info trace message re: starting
	Trace.Indent()
	Trace.WriteLineIf(myTraceSwitch.TraceInfo, _
	 "Executing ImageDirectoryMgr Constructor at: " _ 
	& Now.ToString)
	'check to see if directory exists
	If Directory.Exists(Path:=directoryPath) Then
		Try
				myDirectory = New DirectoryInfo(Path:=directoryPath)
				'write info trace message re: created
				Trace.WriteLineIf(myTraceSwitch.TraceInfo, _
				"Directory instance created.")
		Catch e As Exception
				'write error trace message re: error
				Trace.WriteLineIf(myTraceSwitch.TraceError, _
				"Error creating directory instance. " & _
				e.Message)
				Debug.Fail("Error creating directory  _
				 instance (" & e.Message & ")")
				'throw the exception
				Throw e
		End Try
	Else
		'write warning trace message re: no directory
		Trace.WriteLineIf(myTraceSwitch.TraceWarning, _
			"The path '" & directoryPath & _
			"' could not be found.")
		'throw an exception
		Throw New Exception("Directory not found.")
	End If
	'write info trace message re: finishing
	Trace.WriteLineIf(myTraceSwitch.TraceInfo, _
	"Finished ImageDirectoryMgr Constructor at: " & _
	Now.ToString)
	Trace.Unindent()
End Sub

