Using the Debug Class[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

wing code shows how you might check to
see whether a number typed into a text box was typed in correctly:

Private Sub AssertSample()
  Dim intNum As Integer

  intNum = CInt(Val(txtNumber.Text))


  Debug.Assert(intNum >= 0 And intNum <= 5, _
    "Number must be between 0 and 5")
End Sub

If you run this code and enter 6 into the text box on sample form
(DebugClass.aspx), the assertion will return False. The .NET runtime will
display a message into the Output window like the one shown in Figure 9.24.

Figure 9.24. Debug.Assert displays information like this into the Output
window when the assertion fails.


TIP

Debug.Assert isn't normally used to validate input, as you've seen here.
The validation controls provided by ASP.NET do a better job at that. We've
purposefully selected a simple example here, just to demonstrate how you
might use Debug.Assert. Normally, you'd use Debug.Assert to verify that the
data sent to a procedure meets certain criteria that you just assume梖or
example, that a string isn't too long or that a value that shouldn't be
zero is, in fact, not equal to zero.



The WriteLineIf Method
If you only wish to write a line to the Output window when a specified
condition is true, you can use the WriteLineIf method of the Debug class.
For example, the sample page uses code like the following to check a
condition before writing information to the Output window:

Private Sub WriteLineIfSample()
  Dim intNum As Integer

  intNum = CInt(txtNumber.Text)
  Debug.WriteLineIf(intNum >= 0 And intNum <= 5, _
   "You input a correct number")
End Sub

TIP

If you want to dig a little deeper, you'll find that the Debug class (and
its cousin, the Trace class) are far more powerful than you've seen here.
Each of these classes relies on the concept of "listener" objects梩hat is,
objects that "listen" for output and collect the output for display. By
default, the only listener for the Debug class is the Output window.
Without too much effort, you can create listeners that write output to a
text file or to the Windows Event Log. Although doing this work is beyond
the scope of this material, you can check out the Debug class in the online
help for information on creating other listeners.

本文关键:Using the Debug Class
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top