1. Trang chủ
  2. » Công Nghệ Thông Tin

vb.net book phần 8 doc

79 214 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 79
Dung lượng 0,91 MB

Nội dung

Developing Web Applications • Chapter 10 521 Q: Do we have to copy the code-behind a VB file to production when we are deploying an application? A: No.The code is compiled into the DLL, which is in the bin directory, so copying the DLL is enough. Q: Is Web.config required in the application root directory? A:Web.config is optional and, if present, overrides the default configuration settings. Q: What is the compilation tag in Web.config? A: Inside Web.config,Visual Studio creates a compilation tag with an attribute debug whose value is True.This tag is used to configure the compilation set- tings.When the debug property is set to True,ASP.NET saves the temporary files that are helpful when debugging. For the applications in production, this property should be set to False. Q: Why shouldn’t we use the same name for Web forms that are in different folders? A:VS.NET uses the code-behind technique; for this reason, each Web form inherits a class in the namespace named after the Web form. In a namespace, there can be no duplicate class names.Thus no two Web forms can have the same name, even though they are in different folders. Q: Can any client consume Web services? A:Yes, any client that understands HTTP and XML can consume Web services. www.syngress.com Frequently Asked Questions The following Frequently Asked Questions, answered by the authors of this book, are designed to both measure your understanding of the concepts presented in this chapter and to assist you with real-life implementation of these concepts. To have your questions about this chapter answered by the author, browse to www.syngress.com/solutions and click on the “Ask the Author” form. 153_VBnet_10 8/15/01 11:26 AM Page 521 522 Chapter 10 • Developing Web Applications Q: Does my current ASP code work under ASP.NET? A:Yes, it will work. In order to support backward compatibility, Microsoft introduced a new filename (.ASPX) for ASP.NET. In order to take advantage of .NET Framework, it would be better if you could rewrite your code to ASP.NET. www.syngress.com 153_VBnet_10 8/15/01 11:26 AM Page 522 Optimizing, Debugging, and Testing Solutions in this chapter: ■ Debugging Concepts ■ Code Optimization ■ Testing Phases and Strategies ; Summary ; Solutions Fast Track ; Frequently Asked Questions Chapter 11 523 153_VBnet_11 8/14/01 5:10 PM Page 523 524 Chapter 11 • Optimizing, Debugging, and Testing Introduction When developing an application, program debugging consumes a significant por- tion of development, and the better you understand how to use the debugging tools, the faster you can track bugs down and fix them. In this chapter, we discuss the tools available in Visual Basic .NET to assist you in debugging.You should already be familiar with some of these tools from previous versions of Visual Basic. It is important to understand the tools that are available and how to use them. Debugging will be a little different now that Visual Basic uses exceptions for runtime errors in your program. When you release your applications, you want them to run as robustly as pos- sible. Different aspects of program development can affect the performance of an application. Many of these concepts will be the same as in previous versions of Visual Basic, but you need to understand some new ones in order to optimize your applications.We talk about some issues in your code that can improve per- formance, and we also discuss some runtime performance issues and the best options to choose from when compiling your application. Prior to releasing your applications, you should completely test them.You should not be using your customers to perform testing for you. Generally, testing is initially allocated its fair share of time.As development deadlines slip, however, the testing phase shrinks to make up for it. Most software engineers do not enjoy testing, but it is a very important part of application development.Testing involves different phases, and different personnel are needed for these phases. Independent personnel should perform the final testing because the developers understand how the program works from the inside, and it is harder for them to step back and look at it from a user perspective. Debugging Concepts As a developer working in any size project, there is one guarantee—there will be bugs. Bugs can come in a variety of forms, but you need to consider three main types: ■ Syntax-related These are usually the easiest to catch, especially with advanced development environments like the one provided by Visual Basic.These occur in situations where you might misspell a reserved word or variable name. ■ Runtime errors These occur when your code is syntactically correct (the compiler does not notice anything in error as it prepares to execute www.syngress.com 153_VBnet_11 8/14/01 5:10 PM Page 524 www.syngress.com the application), but an error occurs as the code actually executes. For example, if you were to attempt to execute a method on an object without first instantiating the object, you would get a runtime error. Unless you include some error-handling code, the application may come to a halt.These are still relatively easy to locate in an environment such as the one provided in Visual Basic .NET. ■ Logic errors These are among the most difficult to track down.They occur when you experience unexpected behavior from your application (your program zigs when it should have zagged).These are usually a result of some logical error in the algorithms of your application. Luckily,Visual Basic provides many useful tools that aid in tracking down logic errors. Among the tools available for debugging are watches, breakpoints, the Exceptions window, conditional compilation, and the addition of traces and asser- tions.We have seen most of these in previous versions of Visual Basic. But, because they do offer some new functionality, we cover each of them in their own section. In addition, the Visual Basic IDE provides a comprehensive debugging menu. First, let’s set up our test project that we will use in order to practice the debugging techniques mentioned thus far. (This project and changes made throughout the chapter are included on the CD. See file Chapter 11/Simple Calculator.vbproj.) 1. Start up a session of Visual Basic .NET. 2. Select a Windows application. Make sure the application has one Windows form. 3. Place controls on the form so that the form looks like Figure 11.1. From right to left, place a textbox1, combobox1, textbox2, label1, textbox3, and button1. Set the name of the controls as listed in the Table 11.1. Table 11.1 Simple Calculator Controls Control Name textbox1 txtLeft combobox1 cboOperation textbox2 txtRight label1 label1 textbox3 TxtResult button1 button1 Optimizing, Debugging, and Testing • Chapter 11 525 153_VBnet_11 8/14/01 5:10 PM Page 525 526 Chapter 11 • Optimizing, Debugging, and Testing 4. Right-click on the Forms Designer and select view code. Right below the line that reads Inherits System.WinForms.Form, enter the following two variable declarations: Private intLeftNumber As Integer Private intRightNumber As Integer 5. Enter the following code into the New() method of the form below the Form1 = Me: 'add available operations to combo box cboOperation.Items.add("+") cboOperation.Items.Add("-") cboOperation.Items.Add(chr(247)) cboOperation.Items.Add("*") 6. Select Button1 from the Class Name combo box at the top left of the code view pane.Then in the method name combobox, select the Click() method. In the Button1_Click() method, enter the following code: intLeftNumber = CType(txtleft.Text, Integer) intRightNumber = CType(txtRight.Text, Integer) Call Calculate() 7. Add the code for the final routine: Protected Sub Calculate() Dim tempResult As Integer 'try to do the requested operation Try Select Case cboOperation.SelectedItem www.syngress.com Figure 11.1 User Interface for Debugging Practice Project 153_VBnet_11 8/14/01 5:10 PM Page 526 Optimizing, Debugging, and Testing • Chapter 11 527 Case "+" tempResult = intLeftNumber + intRightNumber Case "-" tempResult = intLeftNumber - intRightNumber Case "*" tempResult = intLeftNumber * intRightNumber Case chr(247) tempResult = CType(intLeftNumber / _ intRightNumber, Integer) End Select Catch e As Exception 'catch any exceptions e.g. Division by zero tempresult = 0 End Try 'display the result of the operation txtResult.Text = CType(tempResult, String) End Sub This completes the setup of our practice project.You should compile it to make sure that everything is in check.You will notice that VB.NET still provides color-coding of keywords and intrinsic functions.This helps to easily identify and read your code. In addition, the VB.NET IDE provides a new feature that enables you to recognize when you may have misspelled a variable name or keyed in something that it does not recognize. For example, find one of the references to the variable intLeftNumber in the code. Change the spelling from intLeftNumber to intLeftumber.You will notice a wavy underline appear under the word.This functionality is similar to what we are accustomed to seeing in Microsoft Word documents. It tells us immediately that there is something that it does not recognize. If you place the mouse pointer over the word, you will see Tool Tip text that gives more detail about the problem. The example application simply performs the designated operation on two integer values. But, in keeping the example simple, we will be able to demonstrate all the beneficial features available to you when debugging your code in Visual Basic .NET. www.syngress.com 153_VBnet_11 8/14/01 5:10 PM Page 527 528 Chapter 11 • Optimizing, Debugging, and Testing Debug Menu The Visual Basic .NET IDE Debug menu provides us with some very useful tools, which are very helpful for debugging in the runtime environment. Each provides a unique way to control execution of your code line-by-line.The tools include Step Into, Step Out, Step Over, and Run To Cursor.We now examine their functionality. Follow these steps: 1. Open the code view for the designer of the simple calculator. In the code, place the cursor on the line where the Button1_Click() method begins. 2. Place a breakpoint there by pressing F9.A breakpoint will halt execution when the compiler reaches this line of code; we cover it in greater detail in the “Breakpoints” section. 3. Run the application by selecting Start from the Debug menu. 4. When the simple calculator loads up, put any numeric value in each of the left and right text boxes. Select the plus sign to indicate addition in the combo box. 5. Select Calculate.You will notice that the execution of the program stops and that the current line where execution stands is indicated with a yellow arrow in the left margin of the code view.This yellow arrow always indicates the next line that will be executed. By using the com- mands in the Debug menu, we can control where the execution will go. 6. Go to the Debug menu now and select Step Into.You will see the yellow arrow move down one line, which means that the previous line executed successfully.This technique can be very useful. It helps you to follow your code one line at a time in order to determine where an error occurs or to see where a value changes. 7. Continue to select the Step Into command until the arrow is on the same line that calls the Calculate() method. At this point, the execution will move into another procedure. We have options here. If we know that the method in question works and is not the source of any error being investigated, then we may choose to Step Over the call. By selecting to Step Over the call, the method will be executed at real time without us seeing the execution line by line. In order to see the code in the method execute line-by-line, we must Step Into the method. www.syngress.com 153_VBnet_11 8/14/01 5:10 PM Page 528 Optimizing, Debugging, and Testing • Chapter 11 529 8. Select the Step Into command from the Debug menu.After you are in a routine, you again have two choices.You can step through the code line by line, or you can Step Out of the method back to the calling method.You can do this by using the Step Out command. 9. Select the Step Out command from the Debug menu.You will see the execution return to the calling method (Button1_Click) one line after the method you are returning from (the Calculate method).All the code in Calculate() is executed before returning to the calling method. In addition to all these tools, you can also use the Run To Cursor option, which is handy in lengthy methods. It gives you the ability to place the cursor on a line within the current method and have the code execute up to the line where the cursor is. NOTE Each of the Debug menu tools has keyboard shortcuts. You can use these debugging techniques very efficiently by becoming familiar with these shortcuts: Step Into F8 Step Over Shift+F8 Step Out Ctrl+Shift+F8 Run To Cursor Ctrl+F8 These features are very useful but are usually used along with the other useful VB.NET IDE debugging tools, which we discuss in the following sections. Watches Watches provide us with a mechanism where we can interact with the actual data that is stored in our programs at runtime.They allow us to see the values of vari- ables and the values of properties on objects. In addition to being able to view these values, you can also assign new values.This can be very handy while step- ping through your code because you can see what would happen if a variable had a different value at a specific point in time. Let’s use our practice project to examine the value of watches: 1. In order to use the Watch window, the application must be in break mode. So again, let’s set a breakpoint in the Button1_Click event. www.syngress.com 153_VBnet_11 8/14/01 5:10 PM Page 529 530 Chapter 11 • Optimizing, Debugging, and Testing 2. Run the application and enter some numbers then press the Calculate button so that we get to our breakpoint. 3. Place the cursor over the intLeftNumber variable names and select Add Watch.You now see a new window appear at the bottom of the IDE called the Watch window (see Figure 11.2). Now, you can see the value of the variable as it changes for as long as it is in scope.The values of variables are only visible while they are in scope, which means that private declarations are only visible in the classes or methods in which they are declared. Publicly declared variables will be visible throughout the applica- tion. In addition to being able to watch these values, we can also change them.Although we have the Watch window available, place the cursor into the Value field for our watch variable. Change the value to 15 and press Enter. Now continue execution of the program.You will see that the final result reflects the change that you made in the Watch window. www.syngress.com Figure 11.2 The Watch Window 153_VBnet_11 8/14/01 5:10 PM Page 530 [...]... out the callstack Clearbook Edit.ClearBookmarks Clears all the bookmarks Code View.ViewCode Displays the code pane for the current designer view Designer View.ViewDesigner Displays the designer pane for the current code view Cmd View.CommandWindow Display the command mode Immed Tools.ImmediateMode Display the Command window in immediate mode www.syngress.com 535 153_VBnet_11 536 8/ 14/01 5:10 PM Page... here,Visual Studio NET provides some built-in conditional compilation options that enable you to actually trace the execution of a deployed application www.syngress.com 537 153_VBnet_11 5 38 8/14/01 5:10 PM Page 5 38 Chapter 11 • Optimizing, Debugging, and Testing Trace For applications where performance is vital, it would be convenient to be able to trace their performance as the end users were using... time-limited evaluation copy so that your product has the opportunity to be fully tested in a live environment before customers spend their hard earned dollars www.syngress.com 547 153_VBnet_11 5 48 8/14/01 5:10 PM Page 5 48 Chapter 11 • Optimizing, Debugging, and Testing Regression Testing Another fact of software development is that what the client wants now is not going to be the same thing that the client... a dialog box such as the one shown in Figure 11 .8, where you can select the performance object that you would like to monitor.Take a look at the list Of particular interest is Memory and Processor By watching counters for these two items, you can see exactly what type of stress your application is putting on the system In www.syngress.com 153_VBnet_11 8/ 14/01 5:11 PM Page 549 Optimizing, Debugging,... and then select Explain A box will appear at the bottom of the dialog box with an explanation of what the counter is doing Figure 11.7 Windows Performance Monitor Figure 11 .8 Add Counters Dialog www.syngress.com 549 153_VBnet_11 550 8/ 14/01 5:11 PM Page 550 Chapter 11 • Optimizing, Debugging, and Testing Summary A lot of competition exists in the world of software development Not only for products on... different phases of testing will help you deliver quality, robust software www.syngress.com 551 153_VBnet_11 552 8/ 14/01 5:11 PM Page 552 Chapter 11 • Optimizing, Debugging, and Testing Frequently Asked Questions The following Frequently Asked Questions, answered by the authors of this book, are designed to both measure your understanding of the concepts presented in this chapter and to assist you... need to have access to the source code www.syngress.com 153_VBnet_12 8/ 16/01 10:26 AM Page 553 Chapter 12 Security Solutions in this chapter: s Security Concepts s Code Access Security s Role-Based Security s Security Policies s Cryptography s Security Tools Summary Solutions Fast Track Frequently Asked Questions 553 153_VBnet_12 554 8/ 16/01 10:26 AM Page 554 Chapter 12 • Security Introduction Security... available and when to use them www.syngress.com 153_VBnet_12 8/ 16/01 10:26 AM Page 555 Security • Chapter 12 Security Concepts As we discuss in the following sections, code access security and role-based security are the most important vehicles to carry the security through your applications and systems However, let it be clear that we are not discussing VB.NET security, but NET security.That is, the security... can place the cursor on that line and press F9 In fact, you can press F9 to toggle the breakpoint on and off.You may also set breakpoints by selecting New Breakpoint from the Debug menu A new feature in VB.NET is the Breakpoints window, which you can bring up by selecting Windows and then Breakpoints from the Debug menu From this window, you can see a list of all the breakpoints currently set in your... the simple calculator window’s form: 1 At the top of the Form class, enter the following declaration: Private myfile As System.IO.Stream Private BoolSwitch As BooleanSwitch www.syngress.com 153_VBnet_11 8/ 14/01 5:10 PM Page 539 Optimizing, Debugging, and Testing • Chapter 11 2 Add this initialization code to the bottom of the New() method: 'set up the trace listener Boolswitch = New BooleanSwitch("General", . filename (.ASPX) for ASP .NET. In order to take advantage of .NET Framework, it would be better if you could rewrite your code to ASP .NET. www.syngress.com 153_VBnet_10 8/ 15/01 11:26 AM Page 522 Optimizing, Debugging,. your code in Visual Basic .NET. www.syngress.com 153_VBnet_11 8/ 14/01 5:10 PM Page 527 5 28 Chapter 11 • Optimizing, Debugging, and Testing Debug Menu The Visual Basic .NET IDE Debug menu provides. these shortcuts: Step Into F8 Step Over Shift+F8 Step Out Ctrl+Shift+F8 Run To Cursor Ctrl+F8 These features are very useful but are usually used along with the other useful VB. NET IDE debugging tools,

Ngày đăng: 14/08/2014, 04:21

TỪ KHÓA LIÊN QUAN