




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
It is useful for students to study effective during the exam times
Typology: Study notes
1 / 223
This page cannot be seen from the preview
Don't miss anything!
Dear Reader,
I wanted to take this opportunity to explain the rationale behind this book showing up on your shelf for free.
Quite some time ago, Sams Publishing determined that the next big thing to hit the programmer/developer community would be Microsoft’s Visual Studio.NET and the .NET Framework. After discussions with many of you, our authors and key Microsoft team members, Sams dedicated itself to a strategy that would support your efforts to learn the .NET Framework as efficiently and as quickly as possible.
A Programmer’s Introduction to Visual Basic.NET is the perfect example of how our strong relationship with Microsoft and our dedication to bring- ing you authors who are already respected sources in the community suc- cessfully blend and show that Sams Publishing is the source for .NET learning.
Bringing you a Beta2 compliant book by May 2001 was not an easy task. Sams called upon a respected author, Craig Utley, to take on this project. Craig holds a unique place in the VB community where he has been devel- oping in VB since version 1.0. He brings years of experience as a trainer, writer, and speaker to this project and gives you the solid reference you need to make the transition from VB to VB.NET.
I hope this book gives you the tools you need to begin to learn VB.NET. I invite your comments and ideas as I work to make Sams the publisher you look to as your .NET learning resource.
On behalf of all of the Sams Publishing team,
Paul Boger Publisher Sams Publishing
E-mail Paul.Boger@samspublishing.com Mail Paul Boger Publisher Sams Publishing 201 West 103rd Street
201 West 103rd Street Indianapolis, IN 46290 USA
- Introduction Foreword viii - 1 Why Should You Move to Visual Basic.NET? - 2 Your First VB.NET Application - 3 Major VB.NET Changes - 4 Building Classes and Assemblies with VB.NET - 5 Inheritance with VB.NET - 6 Database Access with VB.NET and ADO.NET - 7 Building Web Applications with VB.NET and ASP.NET - 8 Building Web Services with VB.NET - 9 Building Windows Services with VB.NET
stranger to Visual Basic: He wrote his first VB application using VB 1.0, and in the years since, has written numerous books and articles on Visual Basic, ASP, and SQL Server programming. Craig also has worked extensively in the IT industry developing custom applications and providing consultancy and training services based around Visual Basic, ASP, COM+, and SQL Server. Adding to Craig’s industry experience, the Microsoft Visual Basic Program Management team—the very people who designed the features of Visual Basic.NET—helped with the technical content of this book. The result is a concise and accurate introduction to Visual Basic.NET, an invaluable resource for the Visual Basic developer who wants to program the Web, use inheritance, access Web Services, upgrade projects, create Windows services, and begin using all the powerful new features of Visual Basic.NET.
When you write Visual Basic code, you join the three million developers who, for the past 10 years, have been the most productive programmers in the industry. With Visual Basic.NET, you enter the growing community of developers who have the most powerful and productive version of Visual Basic ever: a Visual Basic for both Windows and Web application development; a Visual Basic for creating and consum- ing next generation Web services; a Visual Basic that is redefining rapid application development in our connected world.
Ed Robinson Program Manager Microsoft Visual Basic.NET
About the Author
Craig Utley is President of CIOBriefings LLC, a consulting and training firm focused on helping customers develop enterprise-wide solutions with Microsoft tech- nologies. Craig has been using Visual Basic since version 1.0, and he has guided cus- tomers through the creation of highly scalable Web applications using Active Server Pages, Visual Basic, MTS/Component Services, and SQL Server. Craig’s skills in analyzing and designing enterprise-wide solutions have been used by large corpora- tions and start-up companies alike. A frequent conference speaker as well as a book, courseware, and article author, Craig has recently spent much time writing about VB.NET and ASP.NET for both Sams and Volant Training.
Tell Us What You Think!
As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do bet- ter, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.
As an Executive Editor for Sams, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger.
Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.
When you write, please be sure to include this book’s title and author as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book.
Fax: 317-581-
E-mail: feedback@samspublishing.com
Mail: Shelley Kronzek Executive Editor Sams Publishing 201 West 103rd Street Indianapolis, IN 46290 USA
Introduction
Why Does This Book Exist?
This book is meant to give you a head start on the changes from Visual Basic to Visual Basic.NET (VB.NET). Most of the book assumes that you are comfortable with Visual Basic 6.0 (VB6), so the book endeavors to be a quick introduction to the major differences between VB6 and the new VB.NET.
I’ve been using Visual Basic since version 1.0. The most dramatic shift had been in the move from VB3 to VB4, when class modules were introduced, and VB started on its long, slow path to becoming object oriented. For the first time, you could build COM components in VB, leading to an explosion in n-tier application development. VB4 brought COM development to the average programmer, so it was no longer a technology known only to a few C++ developers.
When I first started looking at the differences between VB6 and VB.NET, I realized that the change would be even more significant than it had been from VB3 to VB4. I thought it would be good to put together a book that helped VB6 developers transi- tion to VB.NET. To that end, I pitched the idea for a book named something like Migrating from VB to VB.NET to a couple of different companies. Sams Publishing liked the idea, and one day they called me and asked me about doing a miniature version of the book…in three weeks.
I don’t know who was crazier: Sams, for asking for the book in three weeks, or me, for agreeing to do it. Then, Sams said they were giving the book away, and I thought they had really lost it. Still, the mission was clear: create a book that targets Visual Studio.NET, Beta 1. Then, the day after I finished the book on Beta 1, Sams made the decision to release a book based on Beta 2 instead. Although I can’t say I was thrilled, I think it was the right decision. There were significant changes between Beta 1 and Beta 2. Microsoft says there will be far fewer changes between Beta 2 and the final product, so this book should have a much longer shelf life than a book based on Beta 1.
There is no doubt: VB.NET will be an exciting change for us all. There is so much new material to learn that it can be somewhat daunting at first. However, the benefits of the .NET Framework are significant, and in the end can greatly reduce the effort required today to build enterprise-ready distributed applications.
This book will be followed by a much more comprehensive book based on the final version of Visual Studio.NET (VS.NET). The good news is that, as previously men- tioned, the changes between Beta 2 and the final product should be far less dramatic than those changes between Beta 1 and Beta 2. Having said that, however, realize that there will be changes before Visual Studio is released.
Why Should You Move to
Visual Basic.NET?
One of the most common questions today is, “Why should I move to .NET?” .NET is new, and there are many questions about what it can do for you. From a Visual Basic standpoint, it’s important to understand some of the dramatic benefits that can be achieved by moving to VB.NET.
Visual Basic.NET: A New Framework
Many people have looked at VB.NET and grumbled about the changes. There are significant changes to the language: a new optional error handling structure, namespaces, true inheri- tance, free threading, and many others. Some see these changes as merely a way that Microsoft can place a check mark next to a certain feature and be able to say, “Yeah, we do that.” However, there are good reasons for the changes in VB.NET. The world of applications is changing. This is merely a con- tinuation of what has occurred over the past several years. If you took a Visual Basic 1.0 developer and showed him an n- tier application with an ASP front end, a VB COM compo- nent middle tier, and a SQL Server back end full of stored procedures, it would look quite alien to him. Yet, over the past few years, the vast majority of developers have been using Visual Basic to create COM components, and they have
C H A P T E R 1
Why Should You Move to Visual Basic.NET?
become quite versed in ADO as well. The needs for reusability and centralization (a way to avoid distributing components to the desktop) have driven this move to the n- tier model.
The move to the Web revealed some problems. Scalability was an issue, but more complex applications had other requirements, such as transactions that spanned mul- tiple components, multiple databases, or both. To address these issues, Microsoft cre- ated Microsoft Transaction Services (MTS) and COM+ Component Services. MTS (in Windows NT 4) and Component Services (an updated MTS in Windows 2000) acted as an object-hosting environment, allowing you to gain scalability and distrib- uted transactions with relative ease. However, VB components could not take full advantage of all that Component Services had to offer, such as object pooling, because VB did not support free threading.
In the ASP/VB6 model, Microsoft had developers building a component and then calling it via an ASP. Microsoft realized that it would be a good idea to make the component directly callable over HTTP, so that an application anywhere in the world could use that component. Microsoft threw their support behind SOAP, Simple Object Access Protocol, which allows developers to call a component over HTTP using an XML string, with the data returning via HTTP in an XML string. Components sport URLs, making them as easy to access as any other Web item. SOAP has the advantage of having been a cross-industry standard, and not just a Microsoft creation.
At this point, you might be tempted to think that SOAP is all you need, and that you can just stick with VB6. Therefore it is important to understand what VB.NET gives you, and why it makes sense for you, and many other developers, to upgrade to .NET. For example, you create components and want them to be callable via SOAP, but how do you let people know that those components exist? .NET includes a dis- covery mechanism that allows you to find components that are available to you. You’ll find out more about this mechanism, including the “disco” file, in Chapter 8, “Building Web Services with VB.NET.” .NET also provides many other features, such as garbage collection for freeing up resources, true inheritance for the first time, debugging that works across languages and against running applications, and the ability to create Windows services and console applications.
Before proceeding, it’s important to understand a little bit more about what is meant by “.NET.” There are many “.NETs” here. There is VB.NET, which is the new ver- sion of Visual Basic. There is Visual Studio.NET, an Integrated Development Environment that hosts VB.NET, C#, and C++.NET. Underlying all this is the. NET Framework and its core execution engine, the Common Language Runtime.
In the .NET model, you write applications that target the .NET Framework. This gives them automatic access to such benefits as garbage collection (which destroys
4 C h a p t e r 1 : W h y S h o u l d Yo u M o v e t o V i s u a l B a s i c. N E T?
Finally, you need to realize that the new language is actually going to have a version number on it, although the final name is undecided. It might well be called VB.NET
You have decided you need to move from Visual Basic 6 to VB.NET, and you picked up this book to find out about the changes. Yet, the first thing you see is a chapter about the .NET Framework. Why start with the .NET Framework? The truth is that you cannot understand VB.NET until you understand the .NET Framework. You see, the .NET Framework and VB.NET are tightly intertwined; many of the services you will build into your applications are actually provided by the .NET Framework and are merely called into action by your application.
The .NET Framework is a collection of services and classes. It exists as a layer between the applications you write and the underlying operating system. This is a powerful concept: The .NET Framework need not be a Windows-only solution. The .NET Framework could be moved to any operating system, meaning your .NET applications could be run on any operating system hosting the .NET Framework. This means that you could achieve true cross-platform capabilities simply by creating VB.NET applications, provided the .NET Framework was available for other plat- forms. Although this promise of cross-platform capability is a strong selling point to .NET, there has not yet been any official announcement about .NET being moved to other operating systems.
In addition, the .NET Framework is exciting because it encapsulates much of the basic functionality that used to have to be built into various programming languages. The .NET Framework has the code that makes Windows Forms work, so any lan- guage can use the built-in code in order to create and use standard Windows forms. In addition, Web Forms are part of the framework, so any .NET language could be used to create Web Applications. Additionally, this means that various programming elements will be the same across all languages; a Long data type will be the same size in all .NET languages. This is even more important when it comes to strings and arrays. No longer will you have to worry about whether or not a string is a BStr or a CStr before you pass it to a component written in another language.
The Common Language Runtime
One of the major components of the .NET Framework is the Common Language Runtime, or CLR. The CLR provides a number of benefits to the developer, such as exception handling, security, debugging, and versioning, and these benefits are avail- able to any language built for the CLR. This means that the CLR can host a variety of languages, and can offer a common set of tools across those languages. Microsoft
6 C h a p t e r 1 : W h y S h o u l d Yo u M o v e t o V i s u a l B a s i c. N E T?
has made VB, C++, and C# ”premier” languages for the CLR, which means that these three languages fully support the CLR. In addition, other vendors have signed up to provide implementations of other languages, such as Perl, Python, and even COBOL.
When a compiler compiles for the CLR, this code is said to be managed code. Managed code is simply code that takes advantage of the services offered by the CLR. For the runtime to work with managed code, that code must contain metadata. This metadata is created during the compilation process by compilers targeting the CLR. The metadata is stored with the compiled code and contains information about the types, members, and references in the code. Among other things, the CLR uses this metadata to
The runtime also handles object lifetimes. Just as COM/COM+ provided reference counting for objects, the CLR manages references to objects and removes them from memory when all the references are gone, through the process known as garbage col- lection. Although garbage collection actually gives you slightly less control than you had in VB, you gain some important benefits. For example, your errors should decrease because the number of objects that end up hanging around due to circular references should be reduced or completely eliminated. In addition, garbage collec- tion ends up being much faster than the old way of destroying objects in VB. Instances of objects you create that are managed by the runtime are called managed data. You can interact with both managed and unmanaged data in the same applica- tion, although managed data gives you all the benefits of the runtime.
The CLR defines a standard type system to be used by all CLR languages. This means that all CLR languages will have the same size integers and longs, and they will all have the same type of string—no more worrying about BStrs and CStrs! This standard type system opens up the door for some powerful language interoperability. For example, you can pass a reference of a class from one component to another, even if those components are written in different languages. You also can derive a class in C# from a base class written in VB.NET, or any other combination of lan- guages targeted to the runtime. Don’t forget that COM had a set of standard types as well, but they were binary standards. This meant that with COM, you had language interoperability at run time. With .NET’s type standard, you have language interoper- ability at design time.
T h e C o m m o n L a n g u a g e R u n t i m e 7