The Base Class Library exposes types through several assemblies that are part of the .NET Framework, and you will often need to invoke types from those assemblies. Moreover, although very rich, the BCL cannot define types covering every aspect of application development. This means that you will often need to use types exposed by other assemblies, such as other projects in the same solution or external compiled assemblies.
To use types defined in external assemblies, you need to add a reference in your project to the desired assembly. To accomplish this, right-click the project name in Solution Explorer and click the Add Reference command from the pop-up menu or select the References tab in My Project and click Add. This activates the Add reference dialog, as shown in Figure 3.3.
Figure 3.3 The Add Reference Dialog.
You need to select the assemblies you want to reference. More than one assembly can be selected by pressing Ctrl and then click on the name of the required assembly. The Add Reference dialog is divided into several tabs. The default tab is named .NET and shows a list of all the available assemblies in the Global Assembly Cache.
The Add Reference dialog shows the version number of assemblies; this is useful because you can have different versions of an assembly with the same name. It also shows the full path for the assembly. When you add a reference to an assembly, Solution Explorer updates the References node. For example, if you want to add security features to your applications, you need to add a reference to the System.Security.dll assembly (refer to Figure 3.3), which is part of the Base Class Library. When added, Solution Explorer looks like Figure 3.4.
Figure 3.4 Solution Explorer is updated with the new reference.
You can use in your code types exposed by the specified assemblies that have public visibility. The Add Reference dialog provides other tabs. The Recent tab shows a list of all the most recently used assemblies for faster reuse. The Browse tab enables searching for assemblies that are not registered into the GAC. The Projects tab enables adding references to other projects in the solution. This is typically the case when you have a class library that exposes types that you want to use inside a client application. There is also another tab, named COM, which enables adding references to COM type libraries as we discuss next.
Adding References to COM Libraries
There could be situations in which you might be required to use COM type libraries in your .NET applications, scenario known also as COM Interop. This should be a spare scenario, because .NET and COM are such different architectures, and the second one was not born for working within the first one. Visual Studio 2010 enables you to add references to old type libraries. To accomplish this, you need to select the COM tab in the Add Reference dialog. All the registered COM type libraries will be shown within the dialog, and you can select needed components (see Figure 3.5).
Figure 3.5 Adding a reference to a COM component.
For example, you might want to include the Windows Media Player functionalities in your application; for this purpose you can select the Windows Media Player component and then click OK (see Figure 3.5). Visual Studio will show a reference named WMPLib.dll in Solution Explorer and generate an assembly named Interop.WMPLib.dll. This assembly is a managed wrapper for the Windows Media Player component and will provide managed access to types exposed by the type library. More generally, Visual Studio generates an Interop.AssemblyName.dll assembly (where AssemblyName is the original name of the assembly) for each referenced type library, which is known as Primary Interoperability Assembly (also known as PIAs) and that allows interoperation between .NET and COM architectures. Different from previous versions of .NET Framework and Visual Studio, by default you no longer see the wrapper assemblies included in your build output because of a new feature called Deploy Without Primary Interoperability Assemblies.
Deploy Without PIAs
When deploying applications that reference a COM library, you also must include in your distribution the primary interoperability assemblies. In our example, the PIA is Interop.WMPLib.dll. In Visual Studio 2010 you can avoid including primary interoperability assemblies in your distributions, although you reference those assemblies. This is possible because Visual Studio can embed in your executable only the types that you effectively use from the referenced assembly. This avoids the need of including the assembly itself in the build output and, consequently, in the deployment process. For our sample scenario about including the Windows Media Player component, we could write a small application that has the name of a media file provided by the user and then launches WMP. Listing 3.8 accomplishes this.
Listing 3.8. Using a COM Component in Code
Module Module1 Sub Main() Console.WriteLine("Type the name of a media file:") Dim fileName As String = Console.ReadLine Dim wmp As New WMPLib.WindowsMediaPlayer wmp.openPlayer(fileName) End Sub End Module
In the preceding code, you need to take a look at this simple line:
Dim wmp As New WMPLib.WindowsMediaPlayer
Declaring an instance of the WMPLib.WindowsMediaPlayer class is sufficient for Visual Studio to embed the definition of the WindowsMediaPlayer object inside our executable so that it will not need to include the entire Interop.WMPLib.dll assembly in the build output. As you may imagine, this is a great feature because if you have a large type library and you need to use only a few types, you can save space and preserve performances. The Deploy Without PIAs feature is enabled by default. If you instead prefer to avoid embedding types within your executable and including the primary interoperability assemblies in your build output, you simply need to right-click the referenced assembly in Solution Explorer and then click Properties. Continuing our example, we would need to select WMPLib.dll in Solution Explorer. The Properties window will show a property called Embed Interop Types that is set to True by default (see Figure 3.6).
Figure 3.6 Enabling the Deploy Without PIAs feature setting the Embed Interop Types property.
If you change the value to False, types will no longer be embedded in your executable, and the primary interoperability assemblies will be included in the build output.
The first three chapters of this book provide a necessary overview of tools and features that you must understand before moving on. Now that you have completed the introductory steps, you are ready to get your hands dirty on the core of the Visual Basic 2010 programming language.