(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: September 2002
Section: Debug VS.NET Applications
Authors: Nancy Folsom, Kathleen Dollard

C#	Identify the Error Using .NET's Debugging Windows
Listing 1	This code compiles without error, but when it runs, an array bounds error occurs. Upon review it seems to be correctly packing the correct number of crates. By using the .NET debugging windows, you can identify the problem.

using System;
using System.Diagnostics;
using System.Text;

public class Factory
{
	public Crate[] Shipment;
	public class FruitLoop {}
	public class Box 
	{
		FruitLoop[] FruitLoops;
		public Box() 
		{
			FruitLoops = new 
			FruitLoop[1000];
		}
	}
	public class Crate
	{
		Box[]	Boxes;
		public Crate()
		{
			Boxes = new Box[144];
		}
	}

	public void Pack(int crates)
	{
		// Create a new shipment
		Shipment = new Crate[crates];

		Console.WriteLine("Packing...");

		// Pack the crates
		for (int i=1; i<=crates; i++)
		{
			Shipment[i] = new Crate();
			Console.Write("{0} ",i);
		}
	}
	[STAThread]
	static void Main(string[] args)
	{
		Factory Smithson = 
			new Factory();
		Smithson.Pack(100);
		Console.ReadLine();
	}
}


[[ME/Art: Note the box in the listing - can we create this?]]
VB.NET	Users are Reporting a Bug in This Simple Form
Listing 2	The form displays when the date entered is prior to 1/1/2002, and users complain that some dates "disappear." Try spotting the bug. Evaluating the scenario explained in this article, you're likely to resolve the bug faster with a conscious, or formal, approach to debugging.

Option Strict On
Imports System
Imports System.Diagnostics
Imports System.Data
Imports System.Windows.Forms

Public Class Form1
	Inherits System.Windows.Forms.Form
	Private WithEvents m_dt As New dataTable()

#Region " Windows Form Designer generated code "

	Private Sub Form1_Load( _
			ByVal sender As System.Object, _
			ByVal e As System.EventArgs) _
			Handles MyBase.Load
		BuildData()
		Dim Binding As Windows.Forms.Binding = _
			New Windows.Forms.Binding("Text", _
			m_dt, "TestDate")
		AddHandler Binding.Format, _
			AddressOf FormatDate
		AddHandler Binding.Parse, _
			AddressOf ParseDate
		txtDate.DataBindings.Add(Binding)
		txtName.DataBindings.Add("Text", m_dt, _
			"TestName")
	End Sub

	Private Sub FormatDate(ByVal sender As _
			Object, ByVal e As ConvertEventArgs)
		If Not e.Value Is DBNull.Value Then
			e.Value = String.Format("{0:d}", _
				e.Value)
		End If
	End Sub
	Private Sub ParseDate(ByVal sender As _
			Object, ByVal e As ConvertEventArgs)
		Debug.WriteLine( _
			Convert.ToDateTime(e.Value))
	End Sub
	Private Sub dataTable_ColumnChanging( _
			ByVal sender As Object, _
			ByVal e As DataColumnChangeEventArgs) _
			Handles m_dt.ColumnChanging
		If CDate(e.ProposedValue) < #1/1/2002# Then
			MessageBox.Show( _
				"Date must be after 1/1/2002")
			Throw New _
				System.ArgumentOutOfRangeException()
		End If
	End Sub
	Private Sub BuildData()
		m_dt.Columns.Add(New DataColumn( _
			"TestDate", GetType(String)))
		m_dt.Columns.Add(New DataColumn( _
			"TestName", GetType(String)))
		m_dt.Rows.Add(m_dt.NewRow)
	End Sub

End Class

