Introduction In 2002, Visual Basic took the biggest leap in innovation since it was released, with the introduction of Visual Basic NET (as it was renamed). After more than a decade, Visual Basic was overdue for a major overhaul. But NET goes beyond an overhaul. It changes almost every aspect of software development. From integrating Internet functionality to creating object-oriented frameworks, Visual Basic NET chal- lenged traditional vb developers to learn dramatic new concepts and techniqu 2005 brings us an enhanced Visual Basic language(renamed this time Visual Basic 2005). New features have been added that cement this language's position as a true object-oriented language with Visual Basic 2005, it is still going to be a challenge for the traditional VB6 developers to learn, but it is an easy road and books like this are here to help you on your path. First, it's necessary to learn the differences between Visual Basic 2005 and the older versions. In some cases, the same functionality is implemented in a different way. This was not done arbitrarily-there are good reasons for the changes. But you must be prepared to unlearn old habits and form new ones Next, you must be open to the new concepts. Full object orientation, new component techniques,new visual tools for both local and Internet interfaces -all of these and more must become part of your skill set to effectively develop applications in Visual Basic. In this book, we cover Visual Basic virtually from start to finish. We begin by looking at the. NET framework and end by looking at the best practices for deploying. NET applications. In between, we look at everything from database access to integration with other technologies such as XML, along with investigating the new features in detail. You will see that Visual Basic 2005 has emerged as a powerful et easy-to-use language that will allow you to target the Internet just as easily as the desktop The Importance of visual Basic Early in the adoption cycle of NET, Microsoft,'s new language, C#f, got the lion s share of attention. But as. NET adoption has increased, Visual Basic's continuing importance has also been apparent. Microsoft has publicly stated that they consider Visual Basic the language of choice for applications where deve oper productivity is one of the highest priorities Future development of Visual Basic is emphasizing capabilities that enable access to the whole expanse of the NET Framework in the most productive way, while C# development is emphasizing the experi- ence of writing code. That fits the traditional role of Visual Basic as the language developers use in the real world to create business applications as quickly as possible This difference is more than academic. One of the most important advantages of the. NET Framework is that it allows applications to be written with dramatically less code. In the world of business applica tions, the goal is to concentrate on writing business logic and to eliminate routine coding tasks as much as possible. The value in this new world is not in churning out lots of code -it is in writing robust, use- ful applications with as little code as possible
Introduction In 2002, Visual Basic took the biggest leap in innovation since it was released, with the introduction of Visual Basic .NET (as it was renamed). After more than a decade, Visual Basic was overdue for a major overhaul. But .NET goes beyond an overhaul. It changes almost every aspect of software development. From integrating Internet functionality to creating object-oriented frameworks, Visual Basic .NET challenged traditional VB developers to learn dramatic new concepts and techniques. 2005 brings us an enhanced Visual Basic language (renamed this time Visual Basic 2005). New features have been added that cement this language’s position as a true object-oriented language. With Visual Basic 2005, it is still going to be a challenge for the traditional VB6 developers to learn, but it is an easy road and books like this are here to help you on your path. First, it’s necessary to learn the differences between Visual Basic 2005 and the older versions. In some cases, the same functionality is implemented in a different way. This was not done arbitrarily — there are good reasons for the changes. But you must be prepared to unlearn old habits and form new ones. Next, you must be open to the new concepts. Full object orientation, new component techniques, new visual tools for both local and Internet interfaces — all of these and more must become part of your skill set to effectively develop applications in Visual Basic. In this book, we cover Visual Basic virtually from start to finish. We begin by looking at the .NET Framework and end by looking at the best practices for deploying .NET applications. In between, we look at everything from database access to integration with other technologies such as XML, along with investigating the new features in detail. You will see that Visual Basic 2005 has emerged as a powerful yet easy-to-use language that will allow you to target the Internet just as easily as the desktop. The Importance of Visual Basic Early in the adoption cycle of .NET, Microsoft’s new language, C#, got the lion’s share of attention. But as .NET adoption has increased, Visual Basic’s continuing importance has also been apparent. Microsoft has publicly stated that they consider Visual Basic the language of choice for applications where developer productivity is one of the highest priorities. Future development of Visual Basic is emphasizing capabilities that enable access to the whole expanse of the .NET Framework in the most productive way, while C# development is emphasizing the experience of writing code. That fits the traditional role of Visual Basic as the language developers use in the real world to create business applications as quickly as possible. This difference is more than academic. One of the most important advantages of the .NET Framework is that it allows applications to be written with dramatically less code. In the world of business applications, the goal is to concentrate on writing business logic and to eliminate routine coding tasks as much as possible. The value in this new world is not in churning out lots of code — it is in writing robust, useful applications with as little code as possible. 03_575368 flast.qxd 10/7/05 10:46 PM Page xxv
Introduction Visual Basic is an excellent fit for this type of development, which makes up the bulk of software devel- pment in todays economy. And it will grow to be an even better fit as it is refined and evolved for exactly that purpose. Who is this book for? This book is written to help experienced developers learn about Visual Basic 2005. From those who are just starting the transition from earlier versions to those who have used Visual Basic for a while and need to gain a deeper understanding, this book provides a discussion on the most common program ming tasks and concepts you need. Professional Visual Basic 2005 offers a wide-ranging presentation of Visual Basic concepts, but the. NET Framework is so large and comprehensive that no single book can cover it all. The most important area in which this book does not attempt to be complete is Web development. While chapters discussing the basics of browser-based programming in Visual Basic are included, professional Web developers should instead refer to Professional ASP. NET 2.0 (Wrox Press) What You need to use this book Although, it is possible to create Visual Basic applications using the command-line tools contained in the. NET Framework SDK, you will need Visual Studio 2005(Professional or higher), which includes the NET Framework SDK, to get the most out of this book. You may use Visual Studio NET 2002 or Visual Studio 2003 instead, but there may be cases where much of the lessons will just not work because func- tionalities and capabilities will not be available in these older versions In addition: o Some chapters make use of SQL Server 2005. However, you can also run the example code using Microsoft's SQL Express, which ships with Visual Studio 2005 everal chapters make use of Internet Information Services(IIS). IIS ships with Windows 2003 Server, Windows 2000 Server, Windows 2000 Professional, and Windows XP, although it is not installed by default a Chapter 21 makes use of MSMQ to work with queued transactions. MSMQ ships with Windows 2003 Server, Windows 2000 Server, Windows 2000 Professional, and Windows XP, although it is not installed by default. What does this book cover? Chapter 1, What Is Microsoft. NET?" -This chapter explains the importance of NET and just he much it changes application development. You gain an understanding of why you need nET by looking at what's wrong with the current development technologies, including COM and the dNA architectural model. Then, we look at how. NET corrects the drawbacks by using the common language runtime(CLr)
xxvi Introduction Visual Basic is an excellent fit for this type of development, which makes up the bulk of software development in today’s economy. And it will grow to be an even better fit as it is refined and evolved for exactly that purpose. Who Is This Book For? This book is written to help experienced developers learn about Visual Basic 2005. From those who are just starting the transition from earlier versions to those who have used Visual Basic for a while and need to gain a deeper understanding, this book provides a discussion on the most common programming tasks and concepts you need. Professional Visual Basic 2005 offers a wide-ranging presentation of Visual Basic concepts, but the .NET Framework is so large and comprehensive that no single book can cover it all. The most important area in which this book does not attempt to be complete is Web development. While chapters discussing the basics of browser-based programming in Visual Basic are included, professional Web developers should instead refer to Professional ASP.NET 2.0 (Wrox Press). What You Need to Use This Book Although, it is possible to create Visual Basic applications using the command-line tools contained in the .NET Framework SDK, you will need Visual Studio 2005 (Professional or higher), which includes the .NET Framework SDK, to get the most out of this book. You may use Visual Studio .NET 2002 or Visual Studio 2003 instead, but there may be cases where much of the lessons will just not work because functionalities and capabilities will not be available in these older versions. In addition: ❑ Some chapters make use of SQL Server 2005. However, you can also run the example code using Microsoft’s SQL Express, which ships with Visual Studio 2005. ❑ Several chapters make use of Internet Information Services (IIS). IIS ships with Windows 2003 Server, Windows 2000 Server, Windows 2000 Professional, and Windows XP, although it is not installed by default. ❑ Chapter 21 makes use of MSMQ to work with queued transactions. MSMQ ships with Windows 2003 Server, Windows 2000 Server, Windows 2000 Professional, and Windows XP, although it is not installed by default. What Does This Book Cover? Chapter 1, “What Is Microsoft .NET?”— This chapter explains the importance of .NET and just how much it changes application development. You gain an understanding of why you need .NET by looking at what’s wrong with the current development technologies, including COM and the DNA architectural model. Then, we look at how .NET corrects the drawbacks by using the common language runtime (CLR). 03_575368 flast.qxd 10/7/05 10:46 PM Page xxvi
Introduction Chapter 2, "Introducing Visual Basic 2005 and Visual Studio 2005"-This chapter provides a first look at a Visual Basic application. As we develop this application, you'll take a tour of some of the new fea tures of Visual Studio 2005 Chapter 3, Variables and Types"-This chapter introduces many of the types lonly used Visual Basic. The main goal of this chapter is to familiarize you with value and reference types and to help those with a background in VB6 understand some of the key differences in how variables are defined in Visual basic Chapter 4, Object Syntax Introduction"-This is the first of three chapters that explore object oriented programming in Visual Basic. This chapter will define objects, classes, instances, encapsulation, abstraction, polymorphism, and inheritance Chapter 5, " Inheritance and Interfaces"This chapter examines inheritance and how it can be used within Visual Basic. We create simple and abstract base classes and demonstrate how to create base classes from which other classes can be derived Chapter 6, The Common Language Runtime"This chapter examines the core of the. NET platform, the common language runtime( CLr). The CLR is responsible for managing the execution of code com- piled for the NET platform. We cover versioning and deployment, memory management, cross-language integration, metadata, and the IL Disassembler Chapter 7, Applying Objects and Components"-This chapter puts the theory of ters 4 and 5 into practice. The four defining object-oriented concepts(abstraction, encapsulation, polymorphism, inheritance)are discussed, and we explain how these concepts can be applied in design and develop ment to create effective object-oriented applications. Chapter 8, Generics"-This chapter focuses on one of the biggest enhancements to Visual Basic in this version--generics Generics enables you to make a generic collection that is still strongly typed-pro- viding fewer chances for errors, increasing performance, and giving you Intellisense features when you are working with your collections Chapter 9, " Namespaces"This chapter introduces namespaces and their hierarchical structure. An explanation of namespaces and some common ones are given. In addition, you learn how to create new namespaces, and how to import and alias existing namespaces within projects. This chapter also looks at the new My namespace that was made available in Visual Basic 2005 Chapter 10, " Exception Handling and Debugging"-This chapter covers how error handling and debugging work in Visual Basic 2005 by discussing the Clr exception handler and the new Try Catch.. Finally structure. We also look at error and trace logging, and how you can use these meth- ods to obtain feedback on how your program is working Chapter 11, "Data Access with ADO. NET 2.0"-This chapter focuses on what you will need to know about the ADO.NET object model to be able to build flexible, fast, and scalable data access objects and applications. The evolution of ADO into ADO.NET is explored, and the main objects in ADO. NET that you need to understand in order to build data access into your. NET applications are explained. xxvii
xxvii Introduction Chapter 2, “Introducing Visual Basic 2005 and Visual Studio 2005” — This chapter provides a first look at a Visual Basic application. As we develop this application, you’ll take a tour of some of the new features of Visual Studio 2005. Chapter 3, “Variables and Types” — This chapter introduces many of the types commonly used in Visual Basic. The main goal of this chapter is to familiarize you with value and reference types and to help those with a background in VB6 understand some of the key differences in how variables are defined in Visual Basic. Chapter 4, “Object Syntax Introduction” — This is the first of three chapters that explore objectoriented programming in Visual Basic. This chapter will define objects, classes, instances, encapsulation, abstraction, polymorphism, and inheritance. Chapter 5, “Inheritance and Interfaces” — This chapter examines inheritance and how it can be used within Visual Basic. We create simple and abstract base classes and demonstrate how to create base classes from which other classes can be derived. Chapter 6, “The Common Language Runtime” —This chapter examines the core of the .NET platform, the common language runtime (CLR). The CLR is responsible for managing the execution of code compiled for the .NET platform. We cover versioning and deployment, memory management, cross-language integration, metadata, and the IL Disassembler. Chapter 7, “Applying Objects and Components”— This chapter puts the theory of Chapters 4 and 5 into practice. The four defining object-oriented concepts (abstraction, encapsulation, polymorphism, inheritance) are discussed, and we explain how these concepts can be applied in design and development to create effective object-oriented applications. Chapter 8, “Generics” — This chapter focuses on one of the biggest enhancements to Visual Basic in this version — generics. Generics enables you to make a generic collection that is still strongly typed — providing fewer chances for errors, increasing performance, and giving you Intellisense features when you are working with your collections. Chapter 9, “Namespaces” — This chapter introduces namespaces and their hierarchical structure. An explanation of namespaces and some common ones are given. In addition, you learn how to create new namespaces, and how to import and alias existing namespaces within projects. This chapter also looks at the new My namespace that was made available in Visual Basic 2005. Chapter 10, “Exception Handling and Debugging” — This chapter covers how error handling and debugging work in Visual Basic 2005 by discussing the CLR exception handler and the new Try . . . Catch . . . Finally structure. We also look at error and trace logging, and how you can use these methods to obtain feedback on how your program is working. Chapter 11, “Data Access with ADO.NET 2.0” — This chapter focuses on what you will need to know about the ADO.NET object model to be able to build flexible, fast, and scalable data access objects and applications. The evolution of ADO into ADO.NET is explored, and the main objects in ADO.NET that you need to understand in order to build data access into your .NET applications are explained. 03_575368 flast.qxd 10/7/05 10:46 PM Page xxvii
Introduction Chapter 12, "Using XML in Visual Basic 2005"-This chapter presents the features of the NET Framework that facilitate the generation and manipulation of XML. we describe the. NET Framework's XML-related namespaces, and a subset of the classes exposed by these namespaces is examined in detail This chapter also touches on a set of technologies that utilize XMl, specifically ADO. NET and SQL Server Chapter 13, Security in the NET Framework 2.0"-This chapter examines the additional tools and func- tionality with regard to the security provided by NET Caspol. exe and Permview. exe, which assist in stablishing and maintaining security policies, are discussed. The System Security Permissions namespace is looked at, and we discuss how it relates to managing permissions. Finally, we examine the SystemSecurity. Cryptography namespace and run through some code to demonstrate the capabilities of this namespac Chapter 14, "Windows Forms"-This chapter looks at Windows Forms, concentrating primarily on forms and built-in controls. What is new and what has been changed from the previous versions of isual Basic are discussed, along with the System. Windows Forms namespace Chapter 15, windows Forms Advanced Features"-This chapter looks at some of the more advanced features that are available to you in building your Windows Forms applications Chapter 16, " Building Web Applications"-This chapter explores Web forms and how you can benefit from their use. Using progressively more complex examples, this chapter explains how NET provides the power of Rapid Application Development(normally associated with Windows applications)for the velo pment of Web applicatio Chapter 17, "ASP. NET 2.0 Advanced Features"-This chapter looks at a lot of the new and advanced features that have been made available to you with the latest release of ASP.NET 2.0. Examples of items covered include cross-page posting, master pages, site navigation, personalization, and more Chapter 18, " Assemblies"This chapter examines assemblies and their use within the CLR. The struc- ture of an assembly, what it contains, and the information it contains is examined. Chapter 19, Deployment"-This chapter examines the manifest of the assembly, and its role in deploy ment will be looked at. We also look at what Visual Studio 2005 and the Clr have to offer you when you Chapter 20,"Working with Classic COM and Interfaces"-This chapter discusses COM and. NET omponent interoperability, and what tools are provided to help link the two technologies Chapter 21, " Enterprise Services"-This chapter explores the NET component services-in particular, transaction processing and queued components Chapter 22, " Threading"-This chapter explores threading and explains how the various objects in the. NET Framework enable any of its consumers to develop multithreaded applications. We examine how threads can be created, how they relate to processes, and the differences between multitasking and Chapter 23, "XML Web Services"-This chapter looks at how to create and consume Web services using Visual Basic. The abstract classes provided by the Clr to set up and work with Web services are discussed, as are some of the technologies that support Web services xviii
xxviii Introduction Chapter 12, “Using XML in Visual Basic 2005” —This chapter presents the features of the .NET Framework that facilitate the generation and manipulation of XML. We describe the .NET Framework’s XML-related namespaces, and a subset of the classes exposed by these namespaces is examined in detail. This chapter also touches on a set of technologies that utilize XML, specifically ADO.NET and SQL Server. Chapter 13, “Security in the .NET Framework 2.0” —This chapter examines the additional tools and functionality with regard to the security provided by .NET. Caspol.exe and Permview.exe, which assist in establishing and maintaining security policies, are discussed. The System.Security.Permissions namespace is looked at, and we discuss how it relates to managing permissions. Finally, we examine the System.Security.Cryptography namespace and run through some code to demonstrate the capabilities of this namespace. Chapter 14, “Windows Forms” — This chapter looks at Windows Forms, concentrating primarily on forms and built-in controls. What is new and what has been changed from the previous versions of Visual Basic are discussed, along with the System.Windows.Forms namespace. Chapter 15, “Windows Forms Advanced Features” — This chapter looks at some of the more advanced features that are available to you in building your Windows Forms applications. Chapter 16, “Building Web Applications” — This chapter explores Web forms and how you can benefit from their use. Using progressively more complex examples, this chapter explains how .NET provides the power of Rapid Application Development (normally associated with Windows applications) for the development of Web applications. Chapter 17, “ASP.NET 2.0 Advanced Features” — This chapter looks at a lot of the new and advanced features that have been made available to you with the latest release of ASP.NET 2.0. Examples of items covered include cross-page posting, master pages, site navigation, personalization, and more. Chapter 18, “Assemblies” — This chapter examines assemblies and their use within the CLR. The structure of an assembly, what it contains, and the information it contains is examined. Chapter 19, “Deployment” — This chapter examines the manifest of the assembly, and its role in deployment will be looked at. We also look at what Visual Studio 2005 and the CLR have to offer you when you come to deploy your applications. Chapter 20, “Working with Classic COM and Interfaces” — This chapter discusses COM and .NET component interoperability, and what tools are provided to help link the two technologies. Chapter 21, “Enterprise Services” — This chapter explores the .NET component services — in particular, transaction processing and queued components. Chapter 22, “Threading” — This chapter explores threading and explains how the various objects in the .NET Framework enable any of its consumers to develop multithreaded applications. We examine how threads can be created, how they relate to processes, and the differences between multitasking and multithreading. Chapter 23, “XML Web Services” — This chapter looks at how to create and consume Web services using Visual Basic. The abstract classes provided by the CLR to set up and work with Web services are discussed, as are some of the technologies that support Web services. 03_575368 flast.qxd 10/7/05 10:46 PM Page xxviii
Introduction Chapter 24, "Remoting"-This chapter takes a detailed look at how to use remoting in classic three-tier application design. We look at the basic architecture of remoting and build a basic server and client that uses a singleton object for answering client requests into the business tier. We then look at how to use serialization to return more complex objects from the server to the client, and how to use the call context for passing extra data from the client to the server along with each call without having to change the object model. Chapter 25, " Windows Services"This chapter examines how Visual Basic is used in the production of Windows Services. The creation, installation, running, and debugging of windows Services are covered. Chapter 26, Network Programming"-This chapter takes a look at working with some of the network- ing protocols that are available to you in your development and how to incorporate a wider network into the functionality of your applications. Chapter 27, "Visual Basic and the Internet"-This chapter looks at how to download resources from the Web, how to design your own communication protocols, and how to reuse the Web browser control he Visual Basic Compiler"-This appendix looks at the Visual Basic compiler vbc.exe Appendix B,Visual Basic Resources"-This appendix provides a short list of VB resources that are out there for you Conventions We have used a number of different styles of text and layout in this book to help differentiate between the different kinds of information. Here are examples of the styles we use and an explanation of what they mean Bullets appear indented, with each new bullet marked as follows o New and important words are in italics o Words that appear on the screen in menus such as File or window are in a similar font to the Keyboard strokes are shown like this: Ctrl-A o If you see something like Object, you'll know that it's a filename, object name, or function Code in a gray box is new, important, pertinent code Dim objMyclass as New My class("Hello world") ebug WriteLine(objMyclass ToString)
xxix Introduction Chapter 24, “Remoting” — This chapter takes a detailed look at how to use remoting in classic three-tier application design. We look at the basic architecture of remoting and build a basic server and client that uses a singleton object for answering client requests into the business tier. We then look at how to use serialization to return more complex objects from the server to the client, and how to use the call context for passing extra data from the client to the server along with each call without having to change the object model. Chapter 25, “Windows Services”— This chapter examines how Visual Basic is used in the production of Windows Services. The creation, installation, running, and debugging of Windows Services are covered. Chapter 26, “Network Programming” — This chapter takes a look at working with some of the networking protocols that are available to you in your development and how to incorporate a wider network into the functionality of your applications. Chapter 27, “Visual Basic and the Internet” — This chapter looks at how to download resources from the Web, how to design your own communication protocols, and how to reuse the Web browser control in your applications. Appendix A, “The Visual Basic Compiler” — This appendix looks at the Visual Basic compiler vbc.exe and the functionality it provides. Appendix B, “Visual Basic Resources” — This appendix provides a short list of VB resources that are out there for you. Conventions We have used a number of different styles of text and layout in this book to help differentiate between the different kinds of information. Here are examples of the styles we use and an explanation of what they mean: Bullets appear indented, with each new bullet marked as follows: ❑ New and important words are in italics. ❑ Words that appear on the screen in menus such as File or Window are in a similar font to the one that you see on screen. ❑ Keyboard strokes are shown like this: Ctrl-A. ❑ If you see something like Object, you’ll know that it’s a filename, object name, or function name. Code in a gray box is new, important, pertinent code: Dim objMyClass as New MyClass(“Hello World”) Debug.WriteLine(objMyClass.ToString) 03_575368 flast.qxd 10/7/05 10:46 PM Page xxix