ptg 744 . Internal—An internal class, method, or property can be accessed only by a compo- nent within the same assembly (dll file). Because ASP.NET pages are compiled into different assemblies than the contents of the App_Code folder, you cannot access an internal member of a class outside of the App_Code folder. . Private—A private class, method, or property can be accessed only within the class itself. Visual Basic .NET supports the following access modifiers (also called access levels), which you can use when declaring a class, method, or property: . Public—A Public class, method, or property has no access restrictions. . Protected—A Protected method or property can be accessed only within the class itself or a derived class. . Friend—A Friend class, method, or property can be accessed only by a component within the same assembly (dll file). Because ASP.NET pages are compiled into differ- ent assemblies than the contents of the App_Code folder, you cannot access a Friend member of a class outside of the App_Code folder. . Protected Friend—A Protected Friend method or property can be accessed within the class itself or a derived class, or any other class located in the same assembly. . Private—A Private class, method, or property can be accessed only within the class itself. Using access modifiers is useful when you develop a component library that might be used by other members of your development team (or your future self). For example, you should mark all methods that you don’t want to expose from your component as private. Intellisense and Components Visual Web Developer automatically pops up with Intellisense when you type the names of classes, properties, or methods in Source view. You can add comments that appear in Intellisense to your custom components to make it easier for other developers to use your components. If you add XML comments to a component, the contents of the XML comments appear automatically in Intellisense. For example, the component in Listing 17.21 includes XML comments for its class definition, property definitions, and method definition (see Figure 17.5). CHAPTER 17 Building Components From the Library of Wow! eBook ptg 745 Building Basic Components 17 LISTING 17.21 Employee.cs /// <summary> /// Represents an employee of Acme.com /// </summary> public class Employee { private string _firstName; private string _lastName; /// <summary> /// The employee first name /// </summary> public string FirstName { get { return _firstName; } } /// <summary> /// The employee last name /// </summary> public string LastName { get { return _lastName; } FIGURE 17.5 Adding comments to a component. From the Library of Wow! eBook ptg 746 } /// <summary> /// Returns an employee from the database /// </summary> /// <param name=”id”>The unique employee identifier</param> /// <returns>An instance of the Employee class</returns> public static Employee getEmployee(int id) { return null; } /// <summary> /// Initializes an employee /// </summary> /// <param name=”firstName”>First Name</param> /// <param name=”lastName”>Last Name</param> public Employee(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } } NOTE You can generate an XML documentation file—a file that contains all the XML com- ments—for the components contained in a folder by using the /doc switch with the C# or Visual Basic command-line compiler. The command-line compiler is discussed in the second part of this chapter, “Building Component Libraries.” Using ASP.NET Intrinsics in a Component When you add code to an ASP.NET page, you are adding code to an instance of the Page class. The Page class exposes several ASP.NET intrinsic objects such as the Request, Response, Cache, Session, and Trace objects. If you want to use these objects within a component, you need to do a little more work. Realize that when you create a component, you are not creating an ASP.NET component. In this chapter, we create .NET components, and a .NET component can be used by any type of .NET application, including a Console application or Windows Forms application. CHAPTER 17 Building Components From the Library of Wow! eBook ptg 747 Building Basic Components 17 To use the ASP.NET instrinsics in a component, you need to get a reference to the current HtppContext. The HttpContext object is the one object available behind the scenes through the entire page processing life cycle. You can access the HttpContext object from any user control, custom control, or component contained in a page. NOTE The HttpContext object includes an Items collection. You can add anything to the Items collection and share the thing among all the elements contained in a page. To get a reference to the current HttpContext object, you can use the static (shared) Current property included in the HttpContext class. For example, the component in Listing 17.22 uses the HttpContext object to use both the Session and Trace objects. LISTING 17.22 Preferences.cs using System.Web; public class Preferences { public static string FavoriteColor { get { HttpContext context = HttpContext.Current; context.Trace.Warn(“Getting FavoriteColor”); if (context.Session[“FavoriteColor”] == null) return “Blue”; else return (string)context.Session[“FavoriteColor”]; } set { HttpContext context = HttpContext.Current; context.Trace.Warn(“Setting FavoriteColor”); context.Session[“FavoriteColor”] = value; } } } The Preferences component contains a single property named FavoriteColor. The value of this property is stored in Session state. Anytime this property is modified, the Trace object writes a warning. From the Library of Wow! eBook ptg 748 CHAPTER 17 Building Components You can use the Preferences component in the page contained in Listing 17.23. LISTING 17.23 ShowPreferences.aspx <%@ Page Language=”C#” trace=”true” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> <script runat=”server”> void Page_PreRender() { body1.Style[“background-color”] = Preferences.FavoriteColor; } protected void btnSelect_Click(object sender, EventArgs e) { Preferences.FavoriteColor = ddlFavoriteColor.SelectedItem.Text; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <style type=”text/css”> .content { width:80%; padding:20px; background-color:white; } </style> <title>Show Preferences</title> </head> <body id=”body1” runat=”server”> <form id=”form1” runat=”server”> <div class=”content”> <h1>Show Preferences</h1> <asp:DropDownList id=”ddlFavoriteColor” Runat=”server”> <asp:ListItem Text=”Blue” /> <asp:ListItem Text=”Red” /> <asp:ListItem Text=”Green” /> </asp:DropDownList> <asp:Button From the Library of Wow! eBook ptg 749 Building Component Libraries 17 id=”btnSelect” Text=”Select” Runat=”server” OnClick=”btnSelect_Click” /> </div> </form> </body> </html> After you open the page in Listing 17.23, you can select your favorite color from the DropDownList control. Your favorite color is stored in the Preferences object (see Figure 17.6). FIGURE 17.6 Selecting a favorite color. Building Component Libraries One of the advertised benefits of using components is code reuse. You write a method once, and then you never need to write the same method again. One problem with the components that have been created to this point is that they have all been application-specific. In other words, you cannot reuse the components across multiple websites without copying all the source code from one App_Code folder to another. From the Library of Wow! eBook ptg 750 CHAPTER 17 Building Components If you want to share components among multiple websites, you can no longer take advan- tage of dynamic compilation. To share components, you need to compile the components explicitly in a separate assembly. Compiling Component Libraries You can use a number of methods to compile a set of components into an assembly: . The command-line compiler . C# or Visual Basic Express . The full version of Visual Studio 2010 These options are explored in turn. Using the C# Command-Line Compiler You can use the C# or Visual Basic command-line compiler to compile a source code file, or set of source code files, into an assembly. The C# compiler is located at the following path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe The Visual Basic command-line compiler is located at the following path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe NOTE If you have installed .NET Framework SDK, you can open the SDK Command Prompt from the Microsoft .NET Framework SDK program group. When the command prompt opens, the paths to the C# and Visual Basic .NET compiler are added to the environ- ment automatically. You can use the csc.exe tool to compile any C# source file like this: csc /t:library SomeFile.cs The /t (target) option causes the compiler to create a component library and not a Console or Windows application. When you execute this command, a new file named SomeFile.dll is created, which is the compiled assembly. As an alternative to compiling a single file, you can compile all the source code files in a folder (and every subfolder) like this: csc /t:library /recurse:*.cs /out:MyLibrary.dll From the Library of Wow! eBook ptg 751 Building Component Libraries 17 The /recurse option causes the compiler to compile the contents of all the subfolders. The /out option provides a name for the resulting assembly. Using Visual C# Express You can download a trial edition of Visual C# Express from the Microsoft website (http://www.microsoft.com/express/windows/). Visual C# Express enables you to build Windows applications, Console applications, and class libraries. To create a class library that you can use with an ASP.NET application, you create a Class Library project in Visual C# Express (see Figure 17.7). When you build the project, a new assembly is created. FIGURE 17.7 Creating a Class Library in C# Express. If you need to use ASP.NET classes in your class library, such as the HttpContext class, you need to add a reference to the System.Web.dll assembly to your Class Library project. Select Project, Add Reference and add the System.Web.dll from the .NET tab (see Figure 17.8). NOTE If you are a VB.NET developer, you can download Visual Basic Express from the MSDN website at http://www.microsoft.com/express/windows/. From the Library of Wow! eBook ptg 752 CHAPTER 17 Building Components Using Visual Studio 2010 The easiest way to create a class library that you can share among multiple ASP.NET appli- cations is to use the full version of Visual Studio 2010 instead of Visual Web Developer. Visual Studio 2010 was designed to enable you to easily build enterprise applications. Building class libraries is one of the features you get in Visual Studio 2010 that you don’t get in Visual Web Developer Express. Visual Studio 2010 enables you to add multiple projects to a single solution. For example, you can add both an ASP.NET project and a Class Library project to the same solution. When you update the Class Library project, the ASP.NET project is updated automatically (see Figure 17.9). FIGURE 17.8 Adding a reference to System.Web.dll. FIGURE 17.9 A solution that contains multiple projects. From the Library of Wow! eBook ptg 753 Building Component Libraries 17 Adding a Reference to a Class Library Now that you understand how you can create a class library in a separate assembly, you need to know how you can use this class library in another project. In other words, how do you use the components contained in an assembly within an ASP.NET page? You can make an assembly available to an ASP.NET application in two ways. You can add the assembly to the application’s /Bin folder or you can add the assembly to the Global Assembly Cache. Adding an Assembly to the Bin Folder In general, the best way to use an assembly in an ASP.NET application is to add the assem- bly to the application’s root Bin folder. There is nothing magical about this folder. The ASP.NET Framework automatically checks this folder for any assemblies. If the folder contains an assembly, the assembly is referenced automatically by the ASP.NET application when it is compiled dynamically. If you use Visual Web Developer, you can select the menu option Website, Add Reference to add a new assembly to your application’s Bin folder (see Figure 17.10). Alternatively, you can simply copy an assembly into this folder. (If the folder doesn’t exist, just create it.) FIGURE 17.10 Adding an assembly reference with Visual Web Developer. When you add an assembly to an ASP.NET application’s Bin folder, the assembly is scoped to the application. This means that you can add different versions of the same assembly to different applications without worrying about any conflicts. Furthermore, if you add an assembly to the Bin folder, you can take advantage of XCopy deployment. In other words, if you need to move your website to a new server, you can simply copy all the files in your website from one server to another. As long as you copy your Bin folder, the assembly is available at the new location. From the Library of Wow! eBook . component, you are not creating an ASP. NET component. In this chapter, we create .NET components, and a .NET component can be used by any type of .NET application, including a Console application. The command-line compiler is discussed in the second part of this chapter, “Building Component Libraries.” Using ASP. NET Intrinsics in a Component When you add code to an ASP. NET page, you are. ptg 744 . Internal—An internal class, method, or property can be accessed only by a compo- nent within the same assembly (dll file). Because ASP. NET pages are compiled into different