Data Tips

While you are debugging, a data tip is displayed when the cursor is paused over a variable. Data tips are used with both simple and complex data types. A complex data type is collapsed in the data tip, and you can expand it to view members.

Visualizers

Visualizers display data tips in an alternate format, when the default format is not convenient or is unrelated to the underlying data type. For example, there is a DataSet visualizer and an Extensible Markup Language (XML) visualizer. A dataset object is more than the composition of all its members, which is normally presented in a data tip. Datasets are an abstraction of a hierarchal data source. Viewing the dataset in that format could be helpful. Another example is reading and processing data from an XML data source. Let’s assume that you are getting incorrect results when handling an XML file. Is the problem the data source or the program logic? Seeing the data in XML format, the underlying format, versus an incomprehensible string could help isolate the problem.

Depending on the variable type, data tips have an optional menu that displays available visualizers. Visualizers are presented based on the type being inspected. Different types support different visualizers, if any. When a data tip detects compatible visualizers, the Visualizer menu is displayed automatically with the data tip, which is the magnifying glass. If a magnifying glass is not presented, no visualizers are available for the target type. Available visualizers are listed in the Visualizer menu.

In the following code, myxml is a field that contains XML defining an array of books. This code is found in the btnVisualizer_Click method. Set a breakpoint at the beginning of the method:

private string myxml = "<books><book><title>" +
    "The Gourmet Microwave</title>" +
    "<price>19.95</price>" +
    "</book><book><title>Sushi, Anyone?</title>" +
    "<price>49.99</price></book></books>";

The following code attempts to change the content of the myxml field. A string that contains XML describing another book is added to the myxml string:

string newbook = "<book><title>" +
    "Donis's Great Adventure</title>" +
    "<price>9.95</price>" +
    "</book>";

myxml = myxml.Insert(5, newbook);

A data tip can display the resulting myxml string easily. Viewing myxml as XML, and not as a raw string, provides clarity. Move the cursor over the myxml variable, and a data tip appears. The raw string is displayed. From the data tip’s Visualizer menu, select XML Visualizer. The XML Visualizer opens, as shown in Figure 15-26. The visualizer uncovers a problem, which is that the underlying XML is not well formed. Why is that? The reason is that the additional book was inserted at the wrong location in the myxml string. This error is not easily detected from examining a raw string manually. However, the problem is caught quickly by the XML Visualizer.

The XML Visualizer showing a problem

Figure 15-26. The XML Visualizer showing a problem

The following code is modified to insert the book at the correct location in the myxml string. Now the XML Visualizer correctly displays the XML of the string, as shown in Figure 15-27. You have just found and successfully resolved a bug!

The XML Visualizer showing the corrected myxml string

Figure 15-27. The XML Visualizer showing the corrected myxml string

myxml = myxml.Insert(7, newbook);

The DataSet Visualizer is another example of a useful visualizer. The following code displays rows of a dataset. A breakpoint is placed on the source line that declares the DataRowCollection variable called rows, shown in bold here:

DataSet ds = new DataSet();
ds.ReadXml(@"C:Xml	itles.xml");
DataRowCollection rows = ds.Tables[0].Rows;
foreach (DataRow row in rows)
{
    lblData.Items.Add(row[0].ToString());
}

When the breakpoint is hit, place the cursor over the ds variable, which is an instance of a dataset. A data tip appears. From the data tip’s Visualize menu, choose DataSet Visualizer. The dataset is displayed in the DataSet Visualizer, as shown in Figure 15-28. You can choose the data table to view using the Table drop-down list in this window. In the data table, you can sort the data and make basic changes to the fields in memory.

The DataSet Visualizer

Figure 15-28. The DataSet Visualizer

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.77.54