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

Chapter 3 Debugging Your Code

You can easily add a variable to the Watch window by right-clicking the variable and selecting Add Watch from the context menu. Doing this with the table variable in the previous section will add it to the Watch 1 window as illustrated in Figure 3-28.

Figure 3-28.  The Watch 1 window

Here, you can open the visualizer by clicking the magnifying glass icon or expanding the table variable to view the other properties of the object. I use the Watch window often as it is a convenient way to keep track of several variables at once.

The DebuggerDisplay Attribute

In the previous section, we discussed how to add a variable to the Watch window in Visual Studio. We saw that we can view the value of a variable or variables easily from this single window.

Using the same code we wrote in the previous section, add the variable called lstSubjects to the Watch window, and expand the variable. You will see the values of the lstSubjects variable listed as {VisualStudioDebugging.Subject} in the Value column as seen in Figure 3-29.

186

Chapter 3 Debugging Your Code

Figure 3-29.  The lstSubjects variable values

To view the values of each item in the list, we need to expand the list item (Figure 3-30) and inspect the values.

Figure 3-30.  View list item values

This will quickly become rather tedious, especially when you are dealing with a rather large list, and you are looking for a specific value.

This is where the DebuggerDisplay attribute comes into play. We are going to modify the Subject class.

Ensure that you add the statement using System.Diagnostics to your code file.

Modify your Subject class as in Listing 3-8.

187