Debugging Collections in Your Visual Studio .NET Applications
Provided by: Jim Ferguson, Project Manager
Introduction:
Visual Studio .NET has excellent
debug facilities for examining your program’s variables, objects, and
properties. However, they can be a little tedious to use, and they also only
work while the program is in break mode. Some objects contain collections
which are difficult to refer to by name unless you know all of the key names
of the collection items.
For example, I wanted to be able to determine the column names of a
datatable at run-time. In addition I wanted an easy way to see the values of
those columns for a particular row. I also wanted to be able to see these
values while the form was in run mode rather than break mode.
The following simple function populates a list box with the column names and
values for a single row in a datatable. The function is declared as “shared”
so that it can be called without having to instantiate an object from the
class:
Public
Class DebugHelper
Public
Shared Sub
FillListBoxDataRow( _
ByVal list As
ListBox, _
ByVal row As DataRow)
Dim
col As DataColumn
For
Each col In
row.Table.Columns
list.Items.Add( _
col.ColumnName & ": " & _
row.Item(col.ColumnName).ToString)
Next
End
Sub
End
Class
To use this helper function from anywhere in your application, simply place
a listbox on a form, and call the function with the following line of code:
DebugHelper.FillListBoxDataRow( _
ListBox1, myTable.rows(1))
Pass in the name of the listbox, and a pointer to the row in the table you
wish to examine, and the listbox will be filled with one row each for the
columns in the row.
Return to the tips page
|