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

Chapter 3 Debugging Your Code

through the code and view the values stored in variables. More importantly, you can see how those values change.

Setting aBreakpoint

The most basic task of debugging is setting a breakpoint. Breakpoints mark the lines of code that you want Visual Studio to pause at, allowing you to take a closer look at what the code is doing at that particular point in time. To place a breakpoint in code, click the margin to the left of the line of code you want to inspect as seen in Figure 3-1.

Figure 3-1.  Setting a breakpoint

This line of code is contained in the ValidateLogin() method. The method is called when the user clicks the login button. Press F5 or click Debug Start Debugging to run your application. You can also just click the Start button as shown in Figure 3-2.

Figure 3-2.  The Start button

After you start debugging, and a breakpoint is hit, the debug toolbar in Visual Studio changes as seen in Figure 3-3.

166

Chapter 3 Debugging Your Code

Figure 3-3.  Debug toolbar when breakpoint hit

The Start button now changes to display Continue. Remember, at this point, your code execution is paused in Visual Studio at the breakpoint you set earlier.

In order to step through your code, you can click the step buttons as displayed in Figure 3-4.

Figure 3-4.  Step buttons

From left to right, these buttons are as follows:

•\

Step Into (F11)

•\

Step Over (F10)

•\

Step Out (Shift+F11)

When you step into a method, you jump to the point in the editor where that method’s code is. If you do not want to step into the method, you can click the Step Over button or press F10 to carry on with the next line of code. If you are inside a method and want to step out and continue debugging the calling code, click the Step Out button or press Shift+F11.

Step into Specific

Imagine that we need a method that generates a waybill number based on specific business rules. Then, when the application starts, the text box field is auto populated with the generated waybill number.

The code used to generate the random waybill functionality is listed in Listing 3-1.

167

Chapter 3 Debugging Your Code

Listing 3-1.  Waybill Generation Code

private string GenerateWaybill(string partA, int rndNum) => $"{partA}- {rndNum}-{DateTime.Now.Year}-{DateTime.Now.Month}";

private string WBPartA() => "acme-"; private int WBPartB(int min, int max)

{

var rngCrypto = new RNGCryptoServiceProvider(); var bf = new byte[4];

rngCrypto.GetBytes(bf);

var result = BitConverter.ToInt32(bf, 0); return new Random(result).Next(min, max);

}

In the form load of the tracking application, we then make a call to the GenerateWaybill() method and pass it the other two methods WBPartA() and WBPartB() as parameters as seen in Listing 3-2.

Listing 3-2. Form Load

private void Form1_Load(object sender, EventArgs e)

{

var frmLogin = new Login(); _ = frmLogin.ShowDialog();

txtWaybill.Text = GenerateWaybill(WBPartA(), WBPartB(100,2000));

}

If you had placed a breakpoint on the line of code that contains the GenerateWaybill() method and step into the methods by pressing F11, you would first step into method WBPartA(), then into method WBPartB(), and lastly into the

GenerateWaybill() method.

Did you know that you can choose which method to step into? When the breakpoint is hit, hold down Alt+Shift+F11 and Visual Studio will pop up a menu for you to choose from as seen in Figure 3-5.

168

Chapter 3 Debugging Your Code

Figure 3-5.  Step into specific

Simply select the method you want to step into and off you go.

Run toClick

When you start debugging and you hit a breakpoint, you can jump around quickly within the code by clicking the Run to Click button. While in the debugger, hover your mouse over a line of code as seen in Figure 3-6, and click the Run to Click button that pops up.

Figure 3-6.  Run to Click

169