Introduction

Welcome to the next era in .NET development. .NET has moved from a set of developer-focused tools and a runtime environment to the core of the latest Microsoft operating system. In 2002, Microsoft released .NET and introduced developers to a new paradigm for building applications. For Visual Basic it was not only a new environment, but really a new language.

Visual Basic .NET (as it was initially called) went beyond an overhaul. .NET changed core elements of the syntax affecting every aspect of development with Visual Basic. The entire runtime model shifted to a new common language runtime (CLR) environment and the language went from object-based to object-oriented. Although most people didn't recognize it, we were moving to a new language.

Now with the introduction of Windows RT, Microsoft has again shifted the paradigm. Not so much at the language level, but as it relates to how user interfaces are developed and work. The original runtime environment, although enhanced for .NET 4.5, risks being flagged as being on the path to that fatal “legacy” designator. Windows 8 introduces the concept of Windows Store applications, which are built on a version of the CLR that has different features, and that's important. Because while client applications may view the core CLR as legacy, server-based applications have probably never been in a position to leverage it more.

This book provides details about not only the latest version of Visual Basic and the new .NET Framework 4.5. More important, it carries this coverage to a new platform, Windows RT, and a new class of Windows Store applications. As a result the contents of this book have been turned upside down. This book doesn't just indicate that there are new Windows Store applications, but focuses in directly on how to build and deploy this new class of applications. The result is a very different book from the previous edition.

If you compare this edition to an older edition you'll immediately realize that this edition is visibly smaller. Just as you saw Visual Basic evolve nearly 10 years ago, .NET is going through an evolution of its own. The result was a need to refocus on what this book covers. This has resulted in a sea change with regard to where to focus coverage for Visual Basic.

The most noticeable omission from the book is in fact the original smart client development model—Windows Forms. When Microsoft introduced WPF it informally announced that the era of Windows Forms was ending. It has taken some time, and certainly support will continue for many more years, but the reality is that the evolution of Windows Forms is complete. The information around Windows Forms provided in past editions of this book is essentially complete. While one or two of the chapters do still reference Windows Forms in their samples, by and large the book has moved beyond the use of this technology.

The result was that Billy Hollis, who has a passion for user interface design, agreed to take on the rather significant task of re-imagining how to approach user interface design in a world that includes Windows RT. The new XAML-based interface design chapters are completely redone from the ground up and focused on teaching developers how to approach XAML development from the ground up. The last version of the book approached the user interface model from Windows Forms and transitioning to XAML. However, in this version the approach takes on XAML as the primary user interface development model. As such these chapters address Windows Store application development head-on, not as an afterthought.

However, Windows Forms wasn't alone in being moved into the past. We've eliminated several appendices, Microsoft Office (both VSTO and SharePoint) integration, and references to classic COM. Some, for example, development around Microsoft Office, is facing its own set of changes as Microsoft Office prepares to evolve. Others, such as classic COM and Windows Forms, are no longer technologies that developers should be targeting. We also found ourselves needing to change out how we addressed larger topics such as ASP.NET and Silverlight. The result is that this book is much more focused on building applications using Visual Basic that target Microsoft's core next generation of technologies.

The Future of Visual Basic

Early in the adoption cycle of .NET, Microsoft's new language, C#, got the lion's share of attention. However, as .NET adoption has increased, Visual Basic's continuing importance has also been apparent. Microsoft has publicly stated that it considers Visual Basic to be the language of choice for applications for which developer productivity is one of the highest priorities.

In the past, it was common for Microsoft and others to “target” different development styles; with Visual Studio 2010, Microsoft announced that VB and C# will follow a process of coevolution. As new language features are developed, they will be introduced to both Visual Basic and C# at the same time. This release is the first step in that process, although it's not complete at this time.

Coevolution does not mean that the languages will look the same, but rather that they will support the same capabilities. For example, Visual Basic has XML literals, but that doesn't mean C# will get exactly the same functionality, as C# has the capability to work with XML through the existing framework classes. The old process of first introducing a feature in Visual Basic and then in the next release having C# catch up, and vice versa, is over. As new capabilities and features are introduced, they are being introduced to both Visual Basic and C# at the same time.

This leads to a discussion of the “Roslyn” compiler implementation. It seems like almost five years ago that the first whispers of a new 64-bit Visual Basic compiler implemented with Visual Basic started. Considered the standard for a serious language, implementing a language compiler in the language it compiles has become something of a standard.

However, over time this project evolved. Microsoft, seeing commonalities across the C# and Visual Basic compilation projects, realized that once the core syntax had been consumed the remainder of the compilation process was common across languages. While each implementation language needed a custom solution to handle parsing and interpreting the raw “code,” once that code had been converted to Intermediate Language (IL) the remaining compiler steps were essentially the same.

Suddenly a new concept—the compiler as a service—was created. Code-named Roslyn, this is the future for both Visual Basic and C# compilation. Roslyn takes the traditional compiler as a “black-box” and provides an interface that for interacting with the creation of .NET assemblies. Introducing an API that exposes your components during the compilation process is a powerful tool. Roslyn has been in a technology preview model since well before the release of Visual Studio 2012 and .NET 4.5—however, Microsoft isn't quite ready to declare it ready for prime time. As a result it's still an optional add-on with Visual Studio 2012.

However, even though Roslyn isn't part of Visual Studio 2012, Visual Studio 2012 includes a few paradigm shifts. For starters you'll find that you can now work on projects that targeted older versions of Visual Studio without breaking backward compatibility in those projects. Visual Studio 2012 was designed so that those people who move to the latest tools are limited when working with a team that hasn't fully migrated to that new version.

More important, Visual Studio 2012 comes with a promise of updates. It'll be interesting to see how this plays out over the coming months, but the Visual Studio team has indicated that they will be releasing regular updates to Visual Studio. Update 1 has already been announced as this book goes to print, and the team has indicated that they would like to continue with updates on a quarterly basis. This goes beyond what we've seen in the past, with Power Pack style updates that occurred out of the standard release cycle. Instead we see that Microsoft is committing to keeping Visual Studio on the cutting edge of evolving technology. As the environments we developers need to support change, we can expect that Visual Studio will be adapting and incrementally improving to help us.

While these changes may not involve changes to the core of the .NET framework, we can expect .NET to remain the most productive environment for custom applications. One of the most important advantages of the .NET Framework is that it enables applications to be written with dramatically less code then other alternatives. Originally this was in comparison to older technologies, but today the comparison is as opposed to writing native solutions that support the many different platforms and operating systems you need to support. 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. In other words, of greatest value in this new paradigm is writing robust, useful applications without churning out a lot of code.

Visual Basic is an excellent fit for this type of development, which makes up the bulk of software development in today's economy. Moreover, it will grow to be an even better fit as it is refined and evolves for exactly that purpose.

Who This Book Is For

This book was written to help experienced developers learn Visual Basic. For those who are just starting the transition from other languages or earlier versions to those who have used Visual Basic for a while and need to gain a deeper understanding, this book provides information on the most common programming tasks and concepts you need.

Professional Visual Basic 2012 and .NET 4.5 Programming 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 focus in this book is providing a working knowledge of key technologies that are important to Visual Basic developers. It provides adequate knowledge for a developer to work across both Windows Store applications through WCF services. This book is meant to provide a breadth of knowledge about how to leverage Visual Basic when developing applications. For certain specific technologies, developers may choose to add to their knowledge by following this book with a book dedicated entirely to a single technology area.

What This Book Covers

This book covers Visual Basic from start to finish. It starts by introducing Visual Studio 2010. As the tool you'll use to work with Visual Basic, understanding Visual Studio's core capabilities is key to your success and enjoyment with building .NET applications. In these pages, you have the opportunity to learn everything from database access, Language Integrated Queries (LINQ), and the Entity Framework, to integration with other technologies such as WPF, WCF, and service-based solutions. Along with investigating new features in detail, you'll see that Visual Basic 10 has emerged as a powerful yet easy-to-use language that enables you to target the Internet just as easily as the desktop. This book covers the .NET Framework 4.

How This Book Is Structured

Part I, “Language Constructs and Environment”—The first six chapters of the book focus on core language elements and development tools used by Visual Basic developers. This section introduces Visual Studio 2012, objects, syntax, and debugging.

  • Chapter 1, “Visual Studio 2012”—Start with the environment where you will work with Visual Basic. This chapter looks at the Visual Studio development environment. Introducing a simple WPF application project and reviewing key capabilities like the debugger, this chapter will help you to prepare for and become comfortable with this powerful environment.
  • Chapter 2, “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, as well as on the Windows RT platform. This chapter introduces you to how the different versions of the CLR are in fact closer to different operating systems than to a common environment. You'll learn about versioning and deployment, memory management, cross-language integration, metadata, and the IL Disassembler. The chapter also introduces namespaces and their hierarchical structure. An explanation of namespaces and some common examples are provided. In addition, you learn about custom namespaces, and how to import and alias existing namespaces within projects. This chapter also looks at the My namespace available in Visual Basic.
  • Chapter 3, “Objects and Visual Basic”—This is the first of two chapters that explore object-oriented programming in Visual Basic. This chapter introduces the basics of objects, types, type conversion, reference types, and the key syntax which make up the core of Visual Basic.
  • Chapter 4, “Custom Objects”—This chapter examines creating objects, and describes how they fit within Visual Basic. Starting with inheritance, you create simple and abstract classes and learn how to create base classes from which other classes can be derived. This chapter puts the theory of object-oriented development into practice. The four defining object-oriented concepts (abstraction, encapsulation, polymorphism, inheritance) are described, and you will learn how these concepts can be applied in design and development to create effective object-oriented applications.
  • Chapter 5, “Advanced Language Constructs”—This chapter looks at some of the more advanced language concepts such as lambda expressions, the new Async keyword, and Iterators. Each of these provides key capabilities that are new to Visual Basic 2012, and this new chapter provides details on how you can leverage these new language constructs.
  • Chapter 6, “Exception Handling and Debugging”—This chapter covers how error handling and debugging work in Visual Basic by discussing the CLR exception handler and the TryCatchFinally structure. Also covered are error and trace logging, and how you can use these methods to obtain feedback about how your program is working.

Part II, “Business Objects and Data Access”—The next five chapters, Chapter 7 through Chapter 11, look at common structures used to contain and access data. This includes framework elements such as arrays and collections, XML, database access, and Windows Communication Foundation (WCF) services. These chapters focus on gathering data for use within your applications.

  • Chapter 7, “Arrays, Collections, Generics”—This chapter focuses on introducing arrays and collections as a baseline for having a set of related items. It then expands on these basic structures by exploring generics. Introduced with version 2.0 of the .NET Framework, generics enable strongly typed collections.
  • Chapter 8, “Using XML with Visual Basic”—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.
  • Chapter 9, “ADO.NET and LINQ”—This chapter focuses on what you need to know about the ADO.NET object model in order 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. Additionally, this chapter delves into LINQ to SQL. LINQ offers the capability to easily access underlying data—basically a layer on top of ADO.NET. Microsoft has provided LINQ as a lightweight façade that provides a strongly typed interface to the underlying data stores.
  • Chapter 10, “Data Access with the Entity Framework”—The EF represents Microsoft's implementation of an Entity Relationship Modeling (ERM) tool. Using EF, developers can generate classes to represent the data structures that are defined within SQL Server, and leverage these objects within their applications.
  • Chapter 11, “Services (XML/WCF)”—This chapter looks at how to build service-oriented components that allow for standards-based communications over a number of protocols. WCF is Microsoft's answer for component communications within and outside of the enterprise.

Part III, “Specialized Topics and Libraries”—Chapters 12 through Chapter 14 focus on creating client applications. These chapters address Windows Store applications, which are exclusive to the Windows RT CLR. In parallel it discusses building applications for WPF that are compatible with earlier versions of Windows and which represent the majority of corporate applications. Chapter 14 moves to looking at web-based applications and interfaces. Chapters 15 through 20 then focus on topics such as localization, windows services, security, multi-threaded applications, and deployment.

  • Chapter 12, “XAML Essentials”—Introduced in .NET 3.0, XAML is the syntax originally associated with the Windows Presentation Foundation. With the transition to Windows Store applications, XAML is still applicable, although it behaves slightly differently. This chapter introduces you to building applications focused on the XAML model of user interface declaration.
  • Chapter 13, “Creating XAML Applications for Windows 8”—In this chapter you go deeper into the specifics around building Windows Store applications. The chapter looks at specific conventions around Windows 8 user interfaces, new features specific to Windows 8, and leveraging the visual designer that is part of Visual Studio 2012. This chapter gets into the nuts-and-bolts steps for handling things like Live Tiles and contracts for Windows 8 integration.
  • Chapter 14, “Applications with ASP.NET, MVC, JavaScript, and HTML”—This chapter goes through web-based application development. It covers examples of everything from ASP.NET with AJAX and CSS to MVC (Model-View-Controller) applications.
  • Chapter 15, “Localization”—This chapter looks at some of the important items to consider when building your applications for worldwide use. It looks closely at the System.Globalization namespace and everything it offers your applications.
  • Chapter 16, “Application 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 17, “Assemblies and Reflection”—This chapter examines assemblies and their use within the CLR. The structure of an assembly, what it contains, and the information it contains are described. In addition, you will look at the manifest of the assembly and its role in deployment, and how to use remoting.
  • Chapter 18, “Security in the .NET Framework”—This chapter examines the System.Security.Permissions namespace including how it relates to managing permissions. You also look at the System.Security.Cryptography namespace and run through some code that demonstrates its capabilities.
  • Chapter 19, “Parallel Programming Using Tasks and Threads”—This chapter explores threading and explains how the various objects in the .NET Framework enable any of its consumers to develop multithreaded applications. You will learn how threads can be created, how they relate to processes, and the differences between multitasking and multithreading.
  • Chapter 20, “Deploying XAML Applications via the Windows 8 Windows Store”—This chapter takes a close look at using the Windows Store to deploy applications. You will see the new Windows 8 deployment options and how to set up an account with the Windows Store for deploying your applications. It also looks at how enterprise developers will deploy custom internal line-of-business applications on Windows 8.

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, you'll want Visual Studio 2012, which includes the .NET Framework 4.5, to get the most out of this book. In addition, note the following:

  • You'll need .NET Framework 4.5, which is installed with whatever version of Visual Studio 2012 you select.
  • Some chapters make use of SQL Server 2008. You can run the example code using Microsoft's SQL Express, which is a free download.
  • To build Windows Store applications you'll need to be running Windows 8 or Windows Server 2012. Visual Studio 2012 doesn't require this operating system but building these applications does.

Several chapters make use of Internet Information Services (IIS). IIS is part of every operating system released by Microsoft since Windows XP, but on newer operating systems you'll need to run as administrator to develop against it. Alternatively, you can leverage the development server that ships with Visual Studio 2012.

The source code for the samples is available for download from the Wrox website at:

www.wrox.com/remtitle.cgi?isbn=9781118314456

Conventions

To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.


Warning
Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.


Note
Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

  • We italicize new terms and important words when we introduce them.
  • We show keyboard strokes like this: Ctrl+A.
  • We show filenames, URLs, and code within the text like so: persistence.properties.
  • We present code in two different ways:
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present
 context or to show changes from a previous code snippet.

Source Code

As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at:

www.wrox.com/remtitle.cgi?isbn=9781118314456

You can also search for the book at www.wrox.com by ISBN (the ISBN for this book is 978-1-118-31445-6) to find the code. And a complete list of code downloads for all current Wrox books is available at www.wrox.com/dynamic/books/download.aspx.

At the beginning of each chapter that contains downloadable code, we've provided a reminder of the link you can use to find the code files. Throughout the chapter, you'll also find references to the names of code files in the listing titles or the text.

Most of the code on www.wrox.com is compressed in a .ZIP, .RAR archive, or similar archive format appropriate to the platform. Once you download the code, just decompress it with an appropriate compression tool.


Note
Because many books have similar titles, you may find it easiest to search by ISBN; this book's ISBN is 978-1-118-31445-6.

Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.

Errata

We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or a faulty piece of code, we would be very grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you will be helping us provide even higher-quality information.

To find the errata page for this book, go to

www.wrox.com/remtitle.cgi?isbn=9781118314456

and click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.

If you don't spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We'll check the information and, if appropriate, post a message to the book's errata page and fix the problem in subsequent editions of the book.

p2p.wrox.com

For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At http://p2p.wrox.com, you will find a number of different forums that will help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to http://p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join, as well as any optional information you wish to provide, and click Submit.
4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

Note
You can read messages in the forums without joining P2P, but in order to post your own messages, you must join.

Once you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.

advert

Visit www.safaribooksonline.com/wrox

IFC

Related Wrox Books

book1

Beginning ASP.NET 4.5 in C# and VB

ISBN: 978-1-118-31180-6

Featuring a user-friendly, step-by-step format, the book guides you through customizing the IDE, building a Planet Wrox website example, working with files and web forms, designing web pages, displaying and updating data, creating tables, and much more. With this resource in hand, you will be completely at ease learning ASP.NET 4.5.

book1

Beginning Microsoft Visual C# 2012 Programming

ISBN: 978-1-118-31441-8

Using this book, you will first cover the fundamentals such as variables, flow control, and object-oriented programming and gradually build your skills for web and Windows programming, Windows forms, and data access. Step-by-step directions walk you through processes and invite you to “Try it Out,” at every stage. By the end, you'll be able to write useful programming code following the steps you've learned in this thorough, practical book. If you've always wanted to master Visual C# programming, this book is the perfect one-stop resource.

book1

Ivor Horton's Beginning Visual C++ 2012

ISBN: 978-1-118-36808-4

Offering a comprehensive introduction to both the standard C++ language and to Visual C++, Ivor Horton offers step-by-step programming exercises, examples, and solutions to deftly guide novice programmers through the ins and outs of C++ development.

book1

Professional Visual Studio 2012

ISBN: 978-1-118-33770-7

This book is what you need to get up and running quickly on Visual Studio 2012. Written by a Microsoft Visual C# MVP, it guides you through the integrated development environment (IDE), showing you how to maximize each of the tools and features.

book1

Visual Basic 2012 Programmer's Reference

ISBN: 978-1-118-31407-4

Visual Basic 2012 is packed with powerful features and this book will help you master them all. You'll gain a solid understanding of essential Visual Basic topics and begin applying the information to perform a host of important development tasks. The tutorials inside are filled with code examples and the best methods for using them to write your own programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset