Read an Excerpt
Professional Visual Basic 2010 and .NET 4
By Bill Sheldon Billy Hollis Kent Sharkey Gaston Hillar Rob Windsor Jonathan Marbutt
John Wiley & SonsCopyright © 2010 John Wiley & Sons, Ltd
All right reserved.
Chapter OneVisual Studio 2010
WHAT YOU WILL LEARN IN THIS CHAPTER
* Versions of Visual Studio * An introduction to key Visual Basic terms * Targeting a runtime environment * Creating a baseline Visual Basic Windows Form * Project templates * Project properties - application, compilation, debug * Setting properties * IntelliSense, code expansion, and code snippets * Debugging * Recording and using macros * The Class Designer * Team Foundation Server - Team Explorer
You can work with Visual Basic without Visual Studio. In fact, Appendix A focuses on using the Visual Basic compiler from the command line. In practice, however, most Visual Basic developers treat the two as almost inseparable; without a version of Visual Studio, you're forced to work from the command line to create project files by hand, to make calls to the associated compilers, and to manually address the tools necessary to build your application. While Visual Basic supports this at the same level as C#, F#, C++ and other .NET languages, this isn't the typical focus of a Visual Basic professional.
Visual Basic's success rose from its increased productivity in comparison to other languages when building business applications. Visual Studio 2010 increases your productivity and provides assistance in debugging your applications and is the natural tool for Visual Basic developers.
Accordingly, the current edition of this book is going to start off by introducing you to Visual Studio 2010 and how to build and manage Visual Basic applications. The focus of this chapter is on ensuring that everyone has a core set of knowledge related to tasks like creating and debugging applications in Visual Studio 2010. Visual Studio 2010 will be used throughout the book for building solutions. Note while this is the start, don't think of it as an 'intro' chapter. This chapter will intro key elements of working with Visual Studio, but will also go beyond that. You may find yourself referencing back to it later for advanced topics that you glossed over your first time through. Visual Studio is a powerful and, at times, complex tool and you aren't expected to master it on your first read through this chapter.
When Visual Studio 2005 was released, Microsoft expanded on the different versions of Visual Studio available for use. At the low-cost end, and currently free, is Visual Basic Express Edition. This tool enables you to build desktop applications with Visual Basic only. Its companion for Web development is Visual Web Developer Express, which enables you to build ASP.NET applications. At the high end, Microsoft offers Visual Studio Ultimate. Each of the high-end, Professional, Premium, and Ultimate editions is available as part of an MSDN subscription and each of these editions further extends the core Visual Studio 2010 capabilities beyond the core Integrated Development Environment (IDE) to help improve design, testing, and collaboration between developers.
Of course, the focus of this chapter is how Visual Studio enables you to use Visual Basic to build applications geared toward "better, faster, cheaper" business goals. To this end, we'll be examining features of Visual Studio starting with those in the core Visual Basic 2010 Express Edition and building up to the full Visual Studio Team Suite.
This chapter provides an overview of many of the capabilities of Visual Studio 2010. It also provides a brief introduction to the features available by using one of the more feature-rich versions of Visual Studio. Experienced developers will probably gloss over much of this information although I encourage them to review the new historical debugging features available in Visual Studio 2010 Ultimate covered in this chapter. The goal is to demonstrate how Visual Studio makes you, as a developer, more productive and successful.
VISUAL STUDIO 2010: EXPRESS THROUGH ULTIMATE
For those who aren't familiar with the main elements of .NET development there is the common language runtime (CLR), the .NET Framework, the various language compilers and Visual Studio. Each of these plays a role, for example the CLR - covered in Chapter 4 - manages the execution of code on the .NET platform. Thus code can be targeted to run on a specific version of this runtime environment.
The .NET Framework provides a series of classes that developers leverage across implementation languages. This framework or Class Library is versioned and targeted to run on a specific minimum version of the CLR. It is this library along with the language compilers that are referenced by Visual Studio. Visual Studio allows you to build applications that target one or more of the versions of what is generically called .NET.
In some cases the CLR and the .NET Framework will be the same; for example, .NET Framework version 1.0 ran on CLR version 1.0. In other cases just as Visual Basic's compiler is on version 10, the .NET Framework might have a newer version targeting an older version of the CLR.
The same concepts carry into Visual Studio. Visual Studio 2003 was focused on .NET 1.1, while the earlier Visual Studio .NET (2002) was focused on .NET 1.0. Originally, each version of Visual Studio was optimized for a particular version of .NET. Similarly, Visual Studio 2005 was optimized for .NET 2.0, but then along came the exception of the .NET Framework version 3.0. This introduced a new Framework, which was supported by the same version 2.0 of the CLR, but which didn't ship with a new version of Visual Studio.
Fortunately, Microsoft chose to keep Visual Basic and ASP.NET unchanged for the .NET 3.0 Framework release. However, when you looked at the.NET 3.0 Framework elements, such as Windows Presentation Foundation, Windows Communication Foundation, and Windows Workflow Foundation, you found that those items needed to be addressed outside of Visual Studio. Thus, while Visual Studio is separate from Visual Basic, the CLR and .NET development, in practical terms Visual Studio was tightly coupled to each of these items.
With Visual Studio 2008, Microsoft loosened this coupling by providing robust support that allowed the developer to target any of three different versions of the .NET Framework. Visual Studio 2010 continues this, enabling you to target an application to run on .NET 2.0, .NET 3.0,.NET 3.5, or .NET 4.
However, as you'll discover, this support doesn't mean that Visual Studio 2010 isn't tightly coupled to a specific version of each compiler. In fact, the new support for targeting frameworks is designed to support a runtime environment, not a compile-time environment. This is important because when projects from previous versions of Visual Studio are converted to the Visual Studio 2010 format, they cannot be reopened by a previous version.
The reason for this is that the underlying build engine used by Visual Studio 2010 accepts syntax changes and even language feature changes, but previous versions of Visual Studio do not recognize these new elements of the language. Thus, if you move source code written in Visual Studio 2010 to a previous version of Visual Studio, you face a strong possibility that it would fail to compile. There are ways to manually work with a project across versions of Visual Studio on the same team, but they are not supported. Bill Sheldon, one of the authors of this book, has a blog post from August 2007 that deals with his experience doing this in Visual Studio 2008. The post titled "Working with Both VS 2005 and VS 2008 B2 on the Same Project" is still applicable for those working with Visual Studio 2010: http://nerdnotes .net/blog/default,date,2007-08-29.aspx.
Multi-targeting support by Visual Studio 2010 ensures that your application will run on a specific version of the framework. Thus, if your organization is not supporting .NET 3.0, .NET 3.5, or .NET 4, you can still use Visual Studio 2010. The compiler generates byte code based on the language syntax, and at its core that byte code is version agnostic. Where you can get in trouble is if you reference one or more classes that aren't part of a given version of the CLR. Visual Studio therefore manages your references when targeting an older version of .NET allowing you to be reasonably certain that your application will not reference files from one of those other framework versions. Multi-targeting is what enables you to safely deploy without requiring your customers to download additional framework components they don't need.
With those ground rules in place, what versions of Visual Studio 2010 are available, and what are the primary differences between them? As already mentioned, Visual Basic 2010 Express is at the bottom tier in terms of price and features. It is accompanied there by Visual Web Developer 2010 Express Edition, for those developers who are developing Web applications, rather than desktop applications. These two tools are separate, but both support developing different types of Visual Basic applications, and both are free. Note, however, that neither is extensible; these tools are meant to be introductory, and Microsoft's license prevents vendors from extending these tools with productivity enhancements.
However, each of the Express Edition development tools also ships with two additional components covered briefly here: MSDN Express Edition and SQL Server 2008 Express Edition. MSDN is, of course, the Microsoft Developer Network, which has placed most of its content online. It's the source for not only the core language documentation for Visual Basic, but also articles on almost every product oriented to developers using Microsoft technology. Full versions of Visual Studio ship with the full MSDN library so that you can access its content locally. However, the Express Edition tools actually ship with a pared-down set of documentation files.
Similar to the language and Web-based tools, Microsoft has a SQL Server Express Edition package. This package has a history, in that it replaces the MSDE database engine that was available with SQL Server 2000. The SQL Server Express engine provides the core SQL Server 2008 database engine. For more information on SQL Server Express go to www.microsoft.com/express/database. Note that a free database management application is available via a separate download from Microsoft.
When you install Visual Studio 2010, including the Express Editions, you also have the opportunity to install this core database engine. The elements of this engine are freely redistributable, so if you are looking for a set of core database features based on ADO.NET, you can create your application and deploy your SQL Server 2008 Express Edition database without being concerned about licensing.
Getting back to the differences in versions, the Express Edition tools provide the core components necessary to create Visual Basic applications (Windows or Web) based on the core IDE. Table 1-1 provides a quick summary of what versions are available, including a description of how each extends Visual Studio.
The Express Edition tools are best described as targeting students and hobbyists, not because you can't create serious applications but because they provide only limited support for team development, have limited extensibility, and offer a standalone environment. The Express Tools are oriented toward developers who work independently, while still providing full access to features of the Visual Basic language. This chapter begins working in the IDE using features available in this version, which is essentially the lowest common denominator, and then goes beyond the capabilities of this free tool.
Eventually, however, a developer needs additional tools and projects. This is where the full versions of Visual Studio 2010 (Standard, Professional, Premium and Ultimate) come in. With an increasing level of support for team development, these feature-rich versions add macro support, and, more important, an Object Modeling tool. As discussed in the section titled "Class Diagrams," later in this chapter, Visual Studio enables you to create a visual representation of the classes in your solution and then convert that representation into code. Moreover, the tool supports what is known as round-trip engineering. This means that not only can you use the graphical model to generate code, you can also take a project's source files and regenerate an updated version of the graphical model - that is, edit that model in its graphical format and then update the associated source files.
For those choosing Visual Studio 2008 Professional or above, Visual Studio Tools for Office (VSTO) is targeted primarily at enterprise developers, those who work in corporate organizations (either as employees or consultant/contractors). This tool provides a way for users of the enterprise editions of Microsoft Office 2007 and Microsoft Office 2010 to extend these office productivity tools with application-like features. Many organizations use Microsoft Office for tasks that border on custom applications. This is especially true for Microsoft Excel. VSTO provides project templates based on these Microsoft Office products that enable, for example, a spreadsheet to retrieve its contents from an SQL Server database instead of the local file system. These tools provide the capability not only to manipulate data retrieval and saving, but also to customize the user interface, including direct access to the task pane and custom toolbar options within Microsoft Office products; they are covered in more detail in Chapter 25.
Visual Studio 2010 Premium and Ultimate focus on extending a developer's reach beyond just writing code. These tools are used to examine code for flaws, manage the deployment environment, and define relationships between applications. The high-end versions are focused on tools that support repeatable software processes and best practices. They are geared toward examining source code for hidden flaws that might not cause the code to fail, but might hide a hidden security flaw or make it difficult to maintain or deploy the application. More important, the suite includes tools for creating unit test tools that attempt to cause the code to fail, whether through bad input data or heavy load.
Complete coverage of all of Visual Studio Ultimate's features warrants a book of its own, especially when you take into account all of the collaborative features introduced by Team Foundation Server and its tight integration with both Team Build and SharePoint Server. Team Foundation Server goes beyond just being a replacement for Visual Source Safe. It is the basis for true process-driven development, and it even includes documentation to help train your organization on two process models supported by Microsoft.
VISUAL BASIC KEYWORDS AND SYNTAX
Those with previous experience with Visual Basic are already familiar with many of the language keywords and syntax. However, not all readers will fall into this category so this introductory section is for those new to Visual Basic. A glossary of keywords is provided after which this section will use many of these keywords in context.
Although they're not the focus of the chapter, with so many keywords, a glossary follows. Table 1-2 briefly summarizes most of the keywords discussed in the preceding section, and provides a short description of their meaning in Visual Basic. Keep in mind there are two commonly used terms that aren't Visual Basic keywords that you will read repeatedly including in the glossary:
* Method - A generic name for a named set of commands. In Visual Basic, both subs and functions are types of methods. * Instance - When a class is created, the resulting object is an instance of the class's definition.
Even though the focus of this chapter is on Visual Studio, during this introduction a few basic elements of Visual Basic will be referenced and need to be spelled out. This way as you read, you can understand the examples. Chapter 4, for instance, covers working with namespaces, but some examples and other code are introduced in this chapter that will mention the term, so it is defined here.
Let's begin with namespace. When .NET was being created, the developers realized that attempting to organize all of these classes required a system. A namespace is an arbitrary system that the .NET developers used to group classes containing common functionality. A namespace can have multiple levels of grouping, each separated by a period (.). Thus, the System namespace is the basis for classes that are used throughout .NET, while the Microsoft.VisualBasic namespace is used for classes in the underlying .NET Framework but specific to Visual Basic. At its most basic level, a namespace does not imply or indicate anything regarding the relationships between the class implementations in that namespace; it is just a way of managing the complexity of both your custom application's classes, whether it be a small or large collection, and that of the .NET Framework's thousands of classes. As noted earlier, namespaces are covered in detail in Chapter 4.
Excerpted from Professional Visual Basic 2010 and .NET 4 by Bill Sheldon Billy Hollis Kent Sharkey Gaston Hillar Rob Windsor Jonathan Marbutt Copyright © 2010 by John Wiley & Sons, Ltd. Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.