C# Tutorial MSM_Murach chapter-16-slides

of 17 /17
Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter16 How to organize and docum entyourclasses

Transcript of C# Tutorial MSM_Murach chapter-16-slides

Page 1: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 16

How to organize and document your classes

Page 2: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 2

Objectives Applied 1. Add XML documentation to your classes. 2. Create and use a class library.

Knowledge 1. Describe two ways that you can code two or more classes in a

single file. 2. Describe the benefits of creating and using a class library. 3. Describe the use of namespaces. 4. Describe the use of partial classes.

Page 3: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 3

A file with two classes coded one after the other public class Class1 { // Body of Class1 } public class Class2 { // Body of Class2 }

Page 4: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 4

A file with nested classes public class OuterClass { // code that uses the inner class InnerClass ic = new InnerClass(); // Body of OuterClass public class InnerClass { // Body of InnerClass } }

Page 5: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 5

A Customer class that’s split into two files The first file public partial class Customer { // Some members of the Customer class }

The second file public partial class Customer { // The rest of the members of the Customer class }

Page 6: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 6

A Form class that’s split into two files The Form1.cs file public partial class Form1 : Form { // The code for the Form1 class that's added // by the programmer }

The Form1.designer.cs file partial class Form1 { // The code for the Form1 class that's generated // by Visual Studio }

Page 7: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 7

Code that declares a namespace namespace ProductMaintenance { public partial class Form1 : Form { // Body of Form1 class } }

Page 8: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 8

Code that declares nested namespaces namespace Murach { namespace Validation { // Body of Validation namespace } }

Another way to nest namespaces namespace Murach.Validation { // Body of Validation namespace }

A using statement that specifies a namespace using Murach.Validation;

Page 9: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 9

Part of a Validator class with XML documentation /// <summary> /// Provides static methods for validating data. /// </summary> public class Validator { public Validator() { } /// <summary> /// The title that will appear in dialog boxes. /// </summary> public static string Title = "Entry Error"; /// <summary> /// Checks whether user entered data into a text box. /// </summary> /// <param name="textBox"> /// The text box control to be validated. /// </param>

Page 10: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 10

Part of a Validator class with XML documentation (cont.) /// <returns> /// True if the user has entered data. /// </returns> public static bool IsPresent(TextBox textBox) { if (textBox.Text == "") { MessageBox.Show(textBox.Tag + " is a required field.", Title); textBox.Focus(); return false; } return true; }

Page 11: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 11

XML elements you can use for class documentation Element Description <summary> Provides a general description of a class,

property, method, or other element. <value> Describes the value of a property. <returns> Describes the return value of a method. <param name="name"> Describes a parameter of a method.

Page 12: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 12

A screen tip with documentation for a method

A screen tip with documentation for a parameter

Page 13: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 13

Two projects that use the Validator class

Project 1

Validator(Validation

class)

Form1(Form class)

Project 2

Validator(Validation

class)

Form1(Form class)

Page 14: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 14

Two projects that access the Validator class via a class library

Project 1

Form1(Form class)

ValidationLibrary Project 2

Form1(Form class)

Validator(Validation

class)

Page 15: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 15

A class library project

Page 16: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 16

A project with a reference to a class library

Page 17: C# Tutorial MSM_Murach chapter-16-slides

Murach’s C# 2010, C16 © 2010, Mike Murach & Associates, Inc. Slide 17

A using statement for the validation class library using Murach.Validation;