Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 3 Debugging Your Code

Figure 3-24.  Debugger DataTip

When you pin a DataTip, a pin will appear in the gutter next to the line number. You can now move this DataTip around to another position on the screen. If you look below the pin icon on the DataTip, you will see a “double down arrow” icon. If you click this, you can add a comment to your DataTip as seen in Figure 3-25.

Figure 3-25.  DataTip comment

DataTips also allow you to edit the value of the variable, as long as the value isn’t read-only. To do this, simply select the value in the DataTip and enter a new value. Then press the Enter key to save the new value.

Visualizing Complex Data Types

DataTips also allow you to visualize complex data in a more meaningful way. To illustrate this, we will need to write a little bit of code. We are going to create a class, then create a list of that class, and then create a data table from that list that we will view in the DataTip. I have just created a small Console Application. Start by creating the class in Listing 3-4.

182

Chapter 3 Debugging Your Code

Listing 3-4.  The Subject Class

public class Subject

{

public int SubjectCode { get; set; }

public string SubjectDescription { get; set; }

}

We are going to create a list of the Subject class. Before we do this, however, we need to write the code that is going to create a DataTable of the values in List<Subject>. This code is illustrated in Listing 3-5.

Listing 3-5.  Convert List to DataTable

static DataTable ConvertListToDataTable<T>(List<T> list)

{

var table = new DataTable();

var properties = typeof(T).GetProperties(); foreach (var prop in properties)

{

_ = table.Columns.Add(prop.Name);

}

foreach (var item in list)

{

var row = table.NewRow();

foreach (var property in properties)

{

var name = property.Name;

var value = property.GetValue(item, null); row[name] = value;

}

table.Rows.Add(row);

}

return table;

}

183

Chapter 3 Debugging Your Code

In the Main method, add the code in Listing 3-6. We will then place a breakpoint on the call to the ConvertListToDataTable() method and step over that so that we can inspect the table variable’s DataTip.

Listing 3-6.  Create the List<Subject> and the DataTable

static void Main(string[] args)

{

var lstSubjects = new List<Subject>(); for (var i = 0; i <= 5; i++)

{

var sub = new Subject(); sub.SubjectCode = i; sub.SubjectDescription = $"Subject-{i}"; lstSubjects.Add(sub);

}

var table = ConvertListToDataTable<Subject>(lstSubjects);

}

When you hover over the table variable, you will see that the DataTip displays a magnifying glass icon as seen in Figure 3-26.

Figure 3-26.  The table variable DataTip

If you click the magnifying glass icon, you will see the contents of the table variable displayed in a nice graphical way as seen in Figure 3-27.

184