126Microsoft Visual Studio 2010: A Beginner’s Guide

Within the SolutionDemo solution, we’ll create a new project for a class library.

Right-click SolutionDemo and select Add New Project. This time, select Class Library instead of Console Application and name it ClassLibraryDemo. Clicking OK will add a new Class Library Project to your SolutionDemo Solution. You will now have two projects in your solution.

To use the code in the ClassLibrary project, right-click the ProjectDemo project and select Add Reference. This time, select the Project tab, which will contain all of the projects that belong to the same solution. Select the ClassLibraryDemo project and click OK. You’ll see the reference to ClassLibraryDemo appear in the References folder in the ProjectDemo project.

TIP

Resetting References for Renamed Projects. You can rename any project by right- clicking the project and selecting Rename. However, that doesn’t change the physical folder name. If you want to change the physical folder name, close the solution (select File Close Solution) and then change the project folder name. When you re-open the solution, Solution Explorer won’t be able to load the project. This is because the folder name for the project in the solution file hasn’t changed. To fix this, select the project in Solution Explorer and open the properties window. In the properties window, select the file path property and either type the newly changed path or click the ellipses button to navigate to the *.csproj file. Navigate back to Solution Explorer, right-click the project that didn’t load, and select Reload Project.

Now that you have a reference to a class library, you’ll want to write code that uses the objects in the class library, which you’ll learn about next.

Using Code in Class Libraries

To use class library code, you need to ensure you have a reference to the class library. If using C#, you can add a using directive, and in VB you can add an Imports directive, which allows you to use the types in the class library without fully qualifying them.

After referencing the class library assembly and ensuring namespaces are managed properly, you can use class library classes and instantiate these externally referenced objects and access or invoke the members as if they were part of the code in your own assembly. The .NET CLR will take care of making sure that your calls to the class library object work transparently behind the scenes. The preceding section showed you how to create the reference from one project to another, allowing the compiler to find the other assembly. This section will explain how to write the code that specifies which objects in the class library to use.

Assuming that you were building an educational application, you might have a class library that helped you keep track of students. To facilitate this scenario, you can rename the Class1.cs or Class1.vb file in the ClassLibraryDemo project to Student.cs or Student.vb.

Page 149
Image 149
Microsoft 9GD00001 manual Using Code in Class Libraries