126Microsoft Visual Studio 2010: A Beginner’s Guide
Within the SolutionDemo solution, we’ll create a new project for a class library.
To use the code in the ClassLibrary 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
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.