Why Should I Upgrade?

Why Should I Upgrade?

If it requires work to upgrade your applications from Visual Basic 6 to Visual Basic .NET, you may wonder, Is upgrading worth the trouble? Why should I bother to upgrade an application that requires modifications when it works in Visual Basic 6 today? The main reason for upgrading is to take advantage of the new features of Visual Basic .NET. What are these new features? Listing them all would be a book in itself. The following sections discuss some of the features that people commonly add to their upgraded applications.

New Language Features

Visual Basic .NET adds a number of new language features that make the language more powerful and will forever dispel the myth that Visual Basic is a toy programming language.

Inheritance

For years we developers had been asking Microsoft to add real inheritance to Visual Basic. Sure, Visual Basic 6 supports interface inheritance, but we wanted more; we wanted implementation inheritance. We wanted to benefit from code reuse and to truly implement object-oriented designs. Visual Basic .NET fully supports inheritance, via the new Inherits keyword. You can inherit classes from within your own application, from other applications, and from .NET components written in other languages. You can even use inheritance in forms to inherit the layout, controls, and code of another form. This is called visual inheritance. The following code illustrates the use of the Inherits keyword:

Public Class BaseClass End Class Public Class InheritedClass : Inherits BaseClass End Class

Interfaces in Code

Along with real inheritance, Visual Basic .NET still supports interface inheritance and improves on it by providing the Interface keyword. The Interface keyword defines the interfaces in code. Your classes then implement the interfaces, as in the following example:

Interface myInterface  Function myFunction() End Interface Public Class myImplementedClass  Implements myInterface  Function myFunction() _  Implements myInterface.myFunction      'Some Code  End Function End Class 

Structured Exception Handling

In addition to supporting the familiar On Error GoTo error catching, Visual Basic .NET provides a Try...Catch...End Try exception-handling block that adds error handling. This construct allows you to embed code within an error-handling block. A great use for this type of block is to create a global error handler for your application by including a Try Catch block in the startup object such as Sub Main. In the following example, Sub Main opens a new instance of Form1 and will catch and report any errors that are thrown anywhere in the application:

Sub Main()  Try      Windows.Forms.Application.Run( _      New Form1())  Catch ex As Exception      MsgBox(ex.Message)  End Try End Sub

Arithmetic Operator Shortcuts

All arithmetic operators in Visual Basic .NET now have shortcuts that let you operate on and assign the result back to a variable. For example, in Visual Basic 6, you might write

Dim myString As String myString = myString & "SomeText" 

In Visual Basic .NET, you can write this in a much more elegant format:

Dim myString As String myString &= "SomeText" 

These expression shortcuts apply to &=, *=, +=, -=, /=, \=, and ^=. Note that you can also use the old Visual Basic 6 style expressions.

Overloaded Functions

Visual Basic .NET introduces function overloading. With overloading, you can declare multiple functions with the same name, each accepting a different number of parameters or accepting parameters of different types. For example, suppose that you have a method that deletes a customer from a database. You might want to create two versions of the deleteCustomer method, one to delete a customer based on ID and one to delete a customer by name. You can do so in Visual Basic .NET as follows:

Sub deleteCustomer(ByVal custName As String)  'Code that accepts a String parameter

End Sub Sub deleteCustomer(ByVal custID As Integer)  'Code that accepts an Integer parameter End Sub 

Attributes

Visual Basic .NET also now includes attributes. Attributes give one the ability to fine-tune how an application behaves. They modify the behavior of code elements and can be applied to methods, classes, interfaces, and the application itself. You can use attributes to explicitly declare the GUID for a class or to define how a variable should be marshaled when it is passed to a COM object. Suppose, for example, that you have written a common utility function that you want the debugger always to step over, rather than step into. The DebuggerHidden attribute allows you to do this:

<DebuggerHidden()> _ Function nToz(ByVal input) As Integer If Not IsNumeric(input) Then input = 0 Return Input End Function

Multithreading

By default, your Visual Basic .NET applications are single threaded, but the language has new keywords that allow you to spawn new threads. This ability can be very useful if you have processes that take a long time to complete and that can run in the background. The following example creates a new thread and uses it to run the subroutine loadResultsFromDatabase:

Sub Main()  Dim myThread As New Threading.Thread( _

  AddressOf loadResultsFromDatabase)  mythread.Start() End Sub Sub loadResultsFromDatabase()  'Some Code End Sub

Reduced Programming Errors

Visual Basic .NET helps you reduce programming errors by supporting stronger type checking. For example, if you use the wrong enum value for a property or you assign a variable type to an incompatible type, the compiler will detect and report it at design time. With ADO.NET, you can add strongly typed datasets to your application, and if you refer to an invalid field name, it will be picked up as a compile error instead of a run-time error. These features allow you to catch errors as you write your program. For the ultimate in strong type checking, you can use Option Strict On in your application, which prohibits late binding and ensures that you use conversion functions whenever you assign a variable from one type to another. This feature can be useful in applications that don t use late binding, but it enforces a stricter and more verbose coding standard than many developers are used to.

The .NET Framework

In addition to the familiar VBA library of functions, such as Left$, Right$, and Command$, and the Win32 APIs, Visual Basic .NET has access to the .NET Framework, which is designed specifically for Visual Basic, C#, and the other .NET languages. The .NET Framework is a collection of more than 3800 classes and 28,000 methods for forms, graphics, XML, Internet development, file access, transactions, and almost everything else you can think of. The set of .NET classes you will most likely become familiar with first is the new forms package Windows Forms which looks a lot like the familiar Visual Basic 6 forms but which is implemented as a set of .NET classes available to all languages. The next section describes the features of this package.

Windows Forms

Windows Forms is a new forms development system that replaces the old Visual Basic forms. Along with features that make it powerful and easy to use, Windows Forms is available to all Visual Studio .NET languages.

Faster Development

Windows Forms in Visual Basic .NET has several features that speed up development. An in-place menu editor and visual editing of tab orders make form design easier. Control anchoring allows you to remove all your old resizing code and instead visually anchor the controls so that they remain a fixed length from the edge of the form and resize whenever the form resizes. Visual inheritance allows you to inherit the controls, properties, layout, and code of a base form. A good use for this feature is to define a standard form layout for an application, with a standard size, header, footer, and close button. You can then inherit all forms from this standard form.

GDI+

GDI+ allows you to add rich visual effects to your application. For example, to make a form semitransparent, you would place the following line of code in the form load event:

Me.Opacity = 0.5

Figure 1-1 shows the effects of visual inheritance and GDI+ semitransparency in a Windows application.

Figure 1-1

Visual inheritance and GDI+ semitransparency.

Internationalization

Windows Forms has built-in support for internationalization. To add support for other languages, you set the Localizable property of the form to True, set the Language property to the desired language, and then change the Font and Size properties of the form and controls. Every change that you make is saved as specific to the current locale. For example, you can have different-sized controls with different text for both Spanish and English.

New Web Development Features

Visual Basic .NET offers many enhancements to Web development. Two of the most significant involve XML and Web Forms.

Better Support for XML

Visual Basic .NET has designers that allow visual editing of HTML documents, XML documents, and XML schemas. In addition, there are .NET Framework classes that support serializing and deserializing any .NET class to and from XML. Visual Basic .NET can create XML Web services that use HTTP to pass XML backward and forward to other applications. If your application uses XML, Visual Basic .NET has great support for it. If you re looking to add XML support to your application (or to learn how to use XML), Visual Basic .NET is a great tool for doing so.

Web Services and Web Forms

Visual Basic .NET allows you to add Web services to your application. As you will see later in this book, in many cases you can actually convert your business objects to Web services. You can also easily add a Web Forms presentation layer that leverages your Visual Basic 6 or upgraded business objects. Visual Basic .NET makes Web development as easy as Windows development.

Better Development Environment

Along with language, form, and Web development enhancements, the IDE in Visual Basic .NET has a number of new features.

Cross-Language Interoperability

Visual Basic .NET is designed for cross-language interoperability. Because .NET unifies types, controls and components written in one language can easily be used in another. Anyone who has struggled to get a Visual Basic 6 user control to work in a Visual C++ project will immediately recognize the benefits of this. You can inherit from a class written in any other language built on the .NET platform or create components in other languages that inherit from your Visual Basic classes. Visual Studio .NET provides a single environment for developing and compiling multilanguage applications. Using the unified debugger, you can step from a component written in one language into another. You can also debug COM+ services and step into SQL Server stored procedures.

Background Compiler and Task List

Visual Basic .NET has a compiler that continually works in the background. Compilation errors are flagged in code in real time with blue squiggle underlines. The Task List updates in real time with the list of compilation errors, and it also shows any ToDo comments you add to your code. ToDo comments are a great way to keep track of what you still need to do. For example, if you add a button to a form, and plan to finish the click code later, you can add a comment like

  'TODO: Finish the code later

and the statement appears in the Task List. You can filter the Task List by task type and even choose what sort of comments are shown using the Task List pane. Figure 1-2 shows the Visual Basic .NET Task List.

Figure 1-2

ToDo comments in the Task List.

Is Visual Basic Still the Best Choice for Visual Basic Developers?

If you are faced with learning the new features of Visual Basic .NET, you may ask yourself, Why should I stick with Visual Basic? Why don t I choose another language instead? Why not move to C# (a new language in Visual Studio .NET derived from C++)? Although the choice is yours, we should point out that Visual Basic .NET is now as powerful as C#, Visual C++, or any other language. All the .NET languages have access to the same .NET Framework classes. These classes are powerful, and they allow Visual Basic .NET to smash through the glass ceiling of previous versions.

Visual Basic .NET also keeps the spirit of Visual Basic alive. It is designed by Visual Basic developers for Visual Basic developers, whereas a language like C# is designed for C developers. Each language has some unique features that are not available in other languages. Let s compare C# with Visual Basic to see what makes each language unique.

Features Found in C# but Not in Visual Basic .NET

The C# language supports pointers. Pointers allow you to write unsafe code that modifies memory locations directly. The following code shows how to use pointers in C#:

unsafe static void Main(string[] args) {  int myInt = 5;  int * myptr =  & myInt;  * myptr = 55;  Console.WriteLine(myInt.ToString() ); }

Although Visual Basic .NET doesn t support pointers in the language itself, as you will see in Chapter 16, you can still access memory locations using methods of the .NET Garbage Collector class. C# also supports document comments that allow you to embed self-documenting comments in your source code. These comments are compiled into the metadata of the component and can be extracted and built into help files.

Features Found in Visual Basic .NET but Not in C#

Visual Basic .NET s most appealing feature is the human-readable Visual Basic language, which is case insensitive with great IntelliSense. If you declare a variable as MyVariable and then later change the case to myVariable, Visual Basic .NET automatically changes the case of all occurrences in the code. C# and Visual C++ don t do this. In fact, C# treats MYVariable and myVariable as two separate variables. Most Visual Basic programmers have grown to know and become comfortable with case-insensitive behavior and will find Visual Basic .NET the most natural language to use.

Visual Basic .NET also supports late binding and optional parameters. C# supports neither of these. In addition, Visual Basic supports automatic coercions between types. For example, in C#, you cannot assign a Long to an Integer without using a conversion method (since it may cause an overflow). Visual Basic allows narrowing conversions like this one, since in most cases overflows don t occur. If you want to prevent automatic coercions, you can use a new compiler option, Option Strict On, that enforces C-like strict type coercion.

Visual Basic has richer IntelliSense and better automatic formatting than any other language. It automatically indents code, corrects casing, and adds parentheses to functions when you press Enter.

The result is a true Visual Basic experience, enhanced by background compilation as you type. For example, if you misspell the keyword Function, as in

Funtion myFunction

as soon as you move off the line, the compiler parses it and puts a compiler error in the Task List. It also underlines the word Funtion with a blue squiggle indicating the location of the compile error. As soon as you correct the line, the compiler removes the Task List item and erases the underline. Background compilation helps you write better code and is unique to Visual Basic no other language has a background compiler.

The language you use is a matter of choice. However, if you enjoy programming in Visual Basic, you will find Visual Basic .NET a great experience and the best upgrade ever.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net