Rather than asking what's new, it would almost be easier to ask what's not new in VB .NET. The changes are extensive. We can't cover all the changes from Visual Basic 6.0 in a single chapter, but it's worth taking a look at them in overview here, in one place, so you get an idea of how things are different. We'll note the details of these changes throughout the book.
Be warned: if you are relatively new to Visual Basic, you might just want to skip this section, because there will be a number of advanced topics here that you've probably never seen before—you might want to refer back to this section as these topics become more clear. However, it might prove useful to at least simply skim this section, because besides giving you an outline of what's changed from VB6, it also gives you an outline of the kind of programming we'll see in this book, and what's possible.
Version 4.0 of Visual Basic was a big change, unsettling many programmers, but the change from VB6 to VB .NET is far bigger. Not only has how you design applications and what types of projects you can create changed, but the very syntax of the language has changed a great deal too. Some programmers have complained that Microsoft is trying to turn Visual Basic into Java; if you know Java, you'll see what that means throughout the book in dozens of places, as what was standard in Java has become standard in Visual Basic. One longstanding complaint about Visual Basic has been that it didn't offer a serious programming syntax, and programmers coming from C++ sometimes looked down on VB programmers. Now, however, Microsoft has changed all that, giving the Visual Basic language the same rigor as any other programming language.
This change will take some time for many accomplished VB programmers to come to terms with; for example, everything in Visual Basic is object-oriented now, as it is in Java, and you must declare your variables, as you do in Java, and the built-in VB functionality is encapsulated in a namespace called System, as it is in Java, and so on.
As we've also seen, aside from Windows development, you can also create Web applications in VB .NET. In fact, there's a third alternative now—in addition to Windows applications and Web applications, you can create console applications, as you can in Java. Console applications, as we'll see in this chapter, are command-line oriented applications that run in DOS windows. Those are the three application types with user interfaces: Windows applications, Web applications, and console applications. There's also an additional new type of application—one that doesn't have a built-in user interface: Web services. Web services are applications that run on Web servers and that communicate with other programs, and we'll see them later in this book.
One of the biggest changes in VB .NET is that now everything is object oriented. All items, even variables, are now objects, and we'll see what that means starting in Chapter 2. All aspects of object-oriented programming (OOP) have been implemented in VB .NET, including inheritance, overloading, and polymorphism. Like Java, VB .NET does not support multiple inheritance, but it does support interfaces. We'll get a good look at the OOP issues like these in Chapters 11 and 12.
You can also create multithreaded applications in VB .NET now. A threaded application can do a number of different things at the same time, running different execution threads. These threads can communicate with each other, pass data back and forth, and so on. This means, for example, that when your program is doing something heavily computation-intensive, your program's user interface, running in a different thread, can still respond to the user.
One of the biggest hurdles that VB6 programmers will have to overcome is the great change in Visual Basic syntax. Not only has the fundamental structure of Visual Basic projects like Windows applications become different, but there are a thousand other issues as well—for example, you now have to declare all variables and objects, you must always use parentheses when calling procedures, many keywords are either gone or renamed, there are restrictions on declaring arrays, many old controls are gone and new ones have appeared, strict data typing is now enforced, and so on. All this takes a great deal of effort to get used to, but VB .NET itself will usually tell you what you're doing wrong, and often will explain how to make it right.
In fact, the very data types you can use have changed in VB .NET, because these types have to be the same as you use in other languages like Visual C++ and Microsoft's C#. The reason for this, as we'll see in this chapter, is that the .NET framework that underlies VB .NET uses a Common Language Runtime (CLR) module that applications written in these languages all use.
As you can see, there are plenty of changes in VB .NET. I'll list them in overview in more detail here, starting with Web Development Changes.
The big change in Web development is that you can do it at all. Previous versions of Visual Basic had support for DHTML applications, and other half-hearted attempts, but they never caught on. As you can see, however, Web development is now an integral part of VB .NET. The two major types of Web applications are Web forms and Web services, and we'll cover them both in this book. Here's an overview:
Web forms let you create Web-based applications with user interfaces. These applications are based on ASP.NET (ASP stands for Active Server Pages, Microsoft's Web server technology). You can view these applications in any browser—the application will tailor itself to the browser's capabilities.
You can build Web applications that use standard HTML controls, or new Server controls, that are handled on the Web server. Server controls are displayed using HTML, but execute code on the server.
Web services are made up of code that can be called by other components on the Internet or applications that use Internet protocols. Using Web services, you can send and process data using HTTP and XML messaging standards on the Internet. Web services may be used, for example, as the middle tier of distributed database applications on the Internet.
You can also now check the data a user enters into a Web form using validation controls, which you can add to a Web form while designing it.
You can bind the controls on a Web form to all kinds of different data sources, simply by dragging those sources onto Web forms and setting a few properties.
There are two ways to create your own Web server controls: you can create user controls—Web form pages that are then embedded in other Web form pages. Or, you can create custom server controls using a Software Development Kit (SDK) provided with the .NET Framework.
There have been many ways of handling data in previous versions of Visual Basic, starting with the simple DAO protocol, then RDO, followed by ADO (ActiveX Data Objects). Things have changed again; now you handle data with ADO.NET.
ADO.NET is a new data-handling model that makes it easy to handle data on the Internet. It's also what VB .NET uses on your local machine to communicate with local databases. At the heart of ADO.NET is XML; all data is represented in XML format and exchanged that way. VB .NET can use XML schema to make sure your data is checked for validity before use. This means that much of what you know about ADO, if you're a VB6 programmer, has to be replaced with the corresponding ADO.NET versions (although, for compatibility's sake, you can still use ADO if you need to). For example, you no longer use record sets, but rather datasets. A dataset is a collection of one or more tables or record sets as well as the relationships between those tables or record sets.
Note that ADO is no longer built into Visual Basic; ADO was based on COM protocols, and COM (as well as DCOM) is no longer built into Visual Basic either. Instead, ADO.NET uses XML to exchange data. Both COM and distributed COM (DCOM) technology has been replaced by the .NET framework, although you still have access to COM through what VB .NET calls COM interoperability, if you take special steps to implement it. Here is an overview of what's new in data handling:
Data is handled through ADO.NET, which facilitates development of Web applications.
ADO.NET is built on a disconnected data model that uses snapshots of data that are isolated from the data source. This means you can use and create disconnected, local datasets.
Datasets are based on XML schema, so they can be strongly typed.
There are many new tools and a wizard for handling data in VB .NET, including tools to generate datasets from data connections. You can use the connection wizard or the server explorer to drag and drop whole tables from data sources, as well as creating data adapters, connection objects, and more.
You can now bind any control property to data from a data source.
You can use the data classes provided in the .NET Framework to create and manipulate datasets in code.
You can still work with ADO using the COM interoperability supported in the .NET Framework.
As mentioned earlier, there are many changes to the Visual Basic language itself. VB .NET now supports such OOP features as inheritance, interfaces, and overloading that make it a strong OOP language. You can now create multithreaded applications, and make use of structured exception handling, custom attributes, and more.
Abstraction—The ability to create an abstract representation of a concept in code (as an object named employee is an abstraction of a real employee).
Encapsulation—This has to do with the separation between implementation and interface; that is, when you encapsulate an object, you make its code internal and not accessible to the outside except through a well-defined interface.
Polymorphism—Broadly speaking, this is the ability to create procedures that can operate on objects of different types. For example, if both person and employee objects have a last_name property, a polymorphic procedure can use that property of both objects. Visual Basic handles polymorphism with late binding and multiple interfaces, both of which we'll cover in this book.
Inheritance—Inheritance is the process by which you can derive new classes from other classes. Chapter 12 is all about inheritance in Visual Basic; the idea here is that if you were to create, for example, a class for a specific Visual Basic form and then derive a new type of form from that class, the derived class will inherit all the base's class's functionality, even before you start to customize the derived class by adding new functionality.
VB .NET now supports all these key OOP essentials, and it's become a true OOP language. VB .NET now uses namespaces to prevent naming conflicts by organizing classes, interfaces, and methods into hierarchies, much as Java does. You can import various namespaces to gain access to its classes, interfaces, and methods, and we'll see more on this later in this chapter. For example, the Microsoft. VisualBasic namespace gives you access to common Visual Basic keywords like Asc, Beep,Chr, and so on. The Microsoft.VisualBasic.ControlChars namespace gives you access to common Visual Basic constants like Cr, CrLf, and so on. The Microsoft.VisualBasic.Compatibility.VB6 namespace gives you access to obsolete VB6 keywords such as Open, Close,CreateObject, and so on. The biggest namespace comes from the .NET framework itself, and is called System. For example, the class that supports Windows forms is System.Windows.Forms.Form.
The data types you can use are now restricted to those in the Microsoft Common Language Specification (CLS) as we'll see in the next chapter. On the one hand, that restricts what you can use for data types in Visual Basic, but on the other hand, it also means that any language that is CLS-compliant can use the classes, objects, and components you create in Visual Basic. And you can use classes, components, and objects from other CLS-compliant programming languages. Many new concepts have been added to Visual Basic programming as well, such as assemblies, namespaces, delegates, and attributes, as we'll see in this chapter and in Chapter 2.
Here's an overview of some of the changes to the language—there are too many to list them all here, but we'll see them throughout the book, especially in Chapters 11 and 12, where we cover OOP in VB .NET in depth:
It's all OOP now. All data items are objects now, based on the System.Object class. Even integers and other primitive data types are based on this class. And all code has to be enclosed in a class.
You can now create classes that serve as the base class for derived classes. As mentioned above, this is called inheritance. Derived classes inherit, and can extend, the properties and methods of the base class.
You can now overload properties, methods, and procedures. Overloading means you can define properties, methods, or procedures that have the same name but use different data types. For example, overloaded procedures allow you to provide as many implementations as necessary to handle different kinds of data while giving the appearance of a single procedure.
Visual Basic now supports structured exception handling, using an enhanced version of the Try…Catch…Finally syntax supported by other languages (such as C++).
VB .NET now supports multithreaded applications.
VB .NET now supports constructors and destructors for use when initializing an object of a specific class.
There are three new data types in VB .NET: The Char data type is an unsigned 16-bit type used to store Unicode characters. The Short data type is a signed 16-bit integer (named Integer in earlier Visual Basic versions). Finally, the Decimal data type is a 96-bit signed integer (only available as Variants in earlier versions of Visual Basic).
The Variant type no longer exists; instead, the Object type is the catch-all type. Also, the Currency data type has been replaced by the Decimal type.
VB .NET is now strongly typed; this means you must declare all variables by default, and you can't usually assign one data type to another. You use the CType statement to convert between types. There are also changes in the way you declare variables.
Arrays can no longer be 1-based in VB .NET. That is, the Option Base statement no longer exists.
User-defined types are no longer defined with the Type keyword, but rather with the Structure keyword.
In Visual Basic 6.0, collections were part of the language itself; in VB .NET, they come from the Systems.Collections namespace, so we have to use the ones available in that namespace instead.
There are a number of new compound arithmetic operators, such as +=, -=, •=, /=,\=,^=, and &=. These operators combine two operations in one, as in Java. For example, y += 5 adds 5 to the value in y and assigns the result to y.
If…Then statements are now short-circuited, as in Java, so Visual Basic will evaluate only the operands it needs to determine the result of the statement (see Chapter 2 for more information).
The And, Or, Not, and Xor operators have changed from being bitwise operators to being Boolean operators, which only work on true/false values. The bitwise versions are BitAnd, BitOr, BitNot, and BitXor.
You no longer need to use the Set statement to assign objects to variables.
The syntax for use with procedures has changed—for example, you now always need to use parentheses to enclose the arguments you're passing to a procedure. There are other changes as well; for example, optional parameters now require default values.
In VB6 most parameters were passed by reference; in Visual Basic .NET, the default is passing by value. More on this in Chapter 3.
The syntax for writing property procedures has changed (including the fact that Get and Let have become Get and Set).
Structured exception handling, including Try…Catch…Finally blocks, is now supported.
In VB6, objects had a default property; for example, the default property of a TextBox control was the Text property; you could type TextBox1 = "Hello from Visual Basic" instead of TextBox1.Text = "Hello from Visual Basic". In VB .NET, however, default properties are no longer supported.
Event-handling procedures have changed; now event handlers are passed only two parameters.
The Gosub keyword has been removed.
The DefType keyword has been removed.
Many functions, keywords, and collections from VB6 are now obsolete. Here's a partial list, along with replacements:
Collection has been replaced by System.Collections.
QBColor, RGB have been replaced by System.Drawing.Color.
DateAdd, DateDiff, DatePart, DateSerial, DateValue, TimeSerial, TimeValue have been replaced by elements in System.DateTime
Close, EOF, FileAttr, Get, Put, FreeFile, Input, Line Input, Loc, Lock, LOF, Open, Print, Print Line, Rename, Reset, Seek, SPC, TAB, Unlock, Width, Write, WriteLine have been replaced by elements in System.IO.
The way programs handle COM components has also changed. In VB6, COM components (such as ActiveX controls and documents) were very big. That's all changed now, and some people think that VB .NET is the end of COM, although that's not necessarily so. Although VB .NET does not use COM components internally, it integrates with COM components well. COM components can be treated as .NET components by .NET components, because the .NET framework encloses COM components in a .NET wrapper that .NET components can work with. And, using COM interoperability, COM components also can use .NET components.
As you can see, there are a great many changes going on in VB .NET. Microsoft is aware that the sheer magnitude of all these differences can present a serious obstacle to the adoption of VB .NET (as they say in the documentation, "Visual Basic .NET represents a major departure from previous versions of Visual Basic in several ways."), and has made some efforts to make upgrading from VB6 easier.