Introduction to Managed Code in Visual Studio 2005 Tools for Office
Technology is dominated by two types of people: those who understand what they do not manage, and those who manage what they do not understand.
—PUTT'S LAW
Topics Covered in This Chapter
- What Is Managed Code?
- Introduction to Object-Oriented Programming
- Exploring the .NET Framework
- VSTO and Managed Code
- Summary
- Review Questions
What Is Managed Code?
Code that runs within the .NET Framework is considered managed code. This includes applications written in languages such as Visual C# and Visual Basic 2005. Code that is not managed by the .NET Framework is typically referred to as unmanaged code. This includes applications written in programming languages such as C++, Visual Basic 6.0, and VBA.
All Office solutions created using VSTO are written in managed code. VSTO supports both Visual Basic 2005 and Visual C#; however, we refer only to Visual Basic 2005 in text and in code examples in this book because we believe it is easier to transition from VBA to Visual Basic 2005. Keep in mind that there is much to learn about the .NET Framework and managed code; this chapter only scratches the surface.
Following are some benefits of using managed code:
- Improved security. Often, security permissions are enabled or disabled by end users or determined by the identity of the user who is attempting to run code. However, code managed by the .NET Framework uses the security model code access security (CAS), which is based on the code's identity and location.
- Automatic resource management. Code managed by the .NET Framework eliminates common programming errors such as memory leaks and memory corruption. To eliminate memory leaks, the .NET Framework releases all memory used by the program when it closes.
- Verification of type safety. The .NET Framework ensures that code is type safe before it is run. A programming language that is type safe prevents operations from being performed that are not appropriate for that type.
- Code reuse. The .NET Framework improves productivity by providing a class library that gives you thousands of classes you can use in your code. You can also create your own custom class library.
- Language interoperability. Applications that use managed code can be written in multiple languages that are supported by the .NET Framework. For VSTO, this includes Visual Basic and Visual C#.
- Partial classes. Visual Basic 2005 supports the use of partial classes, thereby separating designer-generated code from your own code. The partial classes are merged into one unified class when the code is compiled.
For VBA developers moving to managed code, an additional advantage is the ability to use all the coding and debugging tools in the Visual Studio IDE and the ability to design solutions using a true object-oriented programming language.