如何使用 Debug.doc_第1页
如何使用 Debug.doc_第2页
如何使用 Debug.doc_第3页
如何使用 Debug.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文介绍如何使用 Debug当程序运行时,您可以使用 Debug 类的方法来生成消息,以帮助您监视程序执行顺序、检测故障或提供性能度量信息。默认情况下,Debug 类产生的消息显示在 Visual Studio 集成开发环境 (IDE) 的“输出”窗口中。该代码示例使用 WriteLine 方法生成后面带有行结束符的消息。当您使用此方法生成消息时,每条消息在“输出”窗口中均显示为单独的一行。使用 Debug 类创建一个示例 使用 Debug 类创建一个示例1. 启动 Visual Studio .NET。 2. 新建一个名为 conInfo 的新 Visual C# .NET 控制台应用程序项目。将创建 Class1。 3. 在 Class1 的顶部添加以下名称空间。 using System.Diagnostics;4. 要初始化变量以使其包含产品的相关信息,请将下面的声明语句添加到 Main 方法: string sProdName = Widget;int iUnitQty = 100;double dUnitCost = 1.03;5. (就在上面代码后面)直接输入将类生成的消息指定为 WriteLine 方法的第一个输入参数。按 CTRL+ALT+O 组合键以确保“输出”窗口可见。 Debug.WriteLine(Debug Information-Product Starting );6. 为了清晰易读,请使用 Indent 方法在“输出”窗口中缩进后面的消息: Debug.Indent();7. 要显示所选变量的内容,请使用 WriteLine 方法,如下所示: Debug.WriteLine(The product name is + sProdName);Debug.WriteLine(The available units on hand are + iUnitQty.ToString();Debug.WriteLine(The per unit cost is + dUnitCost.ToString();8. 您还可以使用 WriteLine 方法显示现有对象的名称空间和类名称。例如,下面的代码在“输出”窗口中显示 System.Xml.XmlDocument 命名空间 System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();Debug.WriteLine(oxml);9、要整理输出,可以包括一个类别作为 WriteLine 方法的第二个可选的输入参数。如果您指定一个类别,则“输出”窗口消息的格式为“类别:消息”。例如,以下代码的第一行在“输出”窗口中显示“Field:The product name is Widget”: Debug.WriteLine(The product name is + sProdName,Field); Debug.WriteLine(The units on hand are + iUnitQty,Field); Debug.WriteLine(The per unit cost is + dUnitCost.ToString(),Field); Debug.WriteLine(Total Cost is + (iUnitQty * dUnitCost),Calc);10. 仅在使用 Debug 类的 WriteLineIf 方法将指定条件计算为 true 时,“输出”窗口才可以显示消息。将要计算的条件是 WriteLineIf 方法的第一个输入参数。WriteLineIf 的第二个参数是仅在第一个参数的条件计算为真时才显示的消息。 Debug.WriteLineIf(iUnitQty 50, This message WILL appear); Debug.WriteLineIf(iUnitQty 1, Message will NOT appear); Debug.Assert(dUnitCost 1, Message will appear since dUnitcost 50, This message WILL appear);Trace.Assert(dUnitCost 1, Message will NOT appear);Trace.Unindent();Trace.WriteLine(Trace Information-Product Ending);Trace.Flush();Console.ReadLine();确认它可以使用1. 确保 Debug 是当前的解决方案配置。 2. 如果“解决方案资源管理器”窗口不可见,请按 CTRL+ALT+L 组合键以显示此窗口。 3. 右键单击“conInfo”,然后单击“属性”。 4. 在 conInfo 属性页左窗格中,在“配置”文件夹下,请确保箭头指向“调试”。 5. 在“配置”文件夹上面的“配置”下拉列表框中,单击“活动(调试)”或“调试”,然后单击“确定”。 6. 按 CTRL+ALT+O 以显示“输出”窗口。 7. 按 F5 键以运行该代码。在出现“断言失败”对话框时,单击“忽略”。 8. 在“控制台”窗口中,按 ENTER 键。此时程序即已完成,“输出”窗口应显示以下输出: Debug Information-Product Starting The product name is Widget The available units on hand are100 The per unit cost is 1.03 System.Xml.XmlDocument Field: The product name is Widget Field: The units on hand are100 Field: The per unit cost is1.03 Calc: Total Cost is 103 This message WILL appear - DEBUG ASSERTION FAILED - - Assert Short Message - Message will appear since dUnitcost 1 is false - Assert Long Message - at Class1.Main(String args) class1.cs(34) The product name is Widget The available units on hand are100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name isWidget This message WILL appear Trace Information-Product Ending 9. “控制台”窗口和 Output.txt 文件应显示以下输出: The product name is Widget The available units on hand are 100 The per unit cost is 1.03 Debug Information-Product Ending Trace Information-Product Starting The product name is Widget Field: The product name is Widget This message WILL appear Trace Information-Product Ending 注意:Output.txt 文件与 conInfo 可执行文件 (conInfo.exe) 位于同一目录中。通常情况下,该目录是存储项目源的 bin 文件夹,默认情况下为 C:Documents and SettingsUser loginMy DocumentsVisual Studio ProjectsconInfobin。完整代码列表 using System;using System.Diagnostics;class Class1STAThreadstatic void Main(string args)string sProdName = Widget;int iUnitQty = 100;double dUnitCost = 1.03;Debug.WriteLine(Debug Information-Product Starting );Debug.Indent();Debug.WriteLine(The product name is +sProdName);Debug.WriteLine(The available units on hand are+iUnitQty.ToString();Debug.WriteLine(The per unit cost is + dUnitCost.ToString();System.Xml.XmlDocument oxml = new System.Xml.XmlDocument();Debug.WriteLine(oxml);Debug.WriteLine(The product name is +sProdName,Field);Debug.WriteLine(The units on hand are+iUnitQty,Field);Debug.WriteLine(The per unit cost is+dUnitCost.ToString(),Field);Debug.WriteLine(Total Cost is +(iUnitQty * dUnitCost),Calc);Debug.WriteLineIf(iUnitQty 50, This message WILL appear);Debug.WriteLineIf(iUnitQty 1, Message will NOT appear);Debug.Assert(dUnitCost 1, Message will appear since dUnitcost 50, This message WILL appear);Trace

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论